SlideShare a Scribd company logo
Hi please find my code.
import java.util.HashMap;
import java.util.Map;
public class Max3Bag {
private HashMap map;
public Max3Bag() {
map = new HashMap<>();
}
public void add(String element){
if(map.containsKey(element)){ // if map already contains this element
if(map.get(element) < 3) // if count is less than 3 then add this element with count
incremented by 1
map.put(element, map.get(element)+1);
}else
map.put(element, 1); // if element is not in map then add it it map with count 1
}
public void remove(String element){
if(map.containsKey(element)){// if element is in map
if(map.get(element) == 1) // if count is 1 then remove from map
map.remove(element);
else // else decrement count by 1
map.put(element, map.get(element)-1);
}
}
public boolean member(String element){
return map.containsKey(element);
}
public void print(){
// get each entry of map and print key and value
for(Map.Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
public class Max3BagTest {
public static void main(String[] args) {
Max3Bag A = new Max3Bag();
A.add("a");
A.add("a");
A.add("a");
A.add("a");
A.add("b");
A.add("b");
A.add("b");
A.remove("a");
A.add("c");
A.print();
}
}
/*
Sample Output:
a=2
b=3
c=1
*/
Solution
Hi please find my code.
import java.util.HashMap;
import java.util.Map;
public class Max3Bag {
private HashMap map;
public Max3Bag() {
map = new HashMap<>();
}
public void add(String element){
if(map.containsKey(element)){ // if map already contains this element
if(map.get(element) < 3) // if count is less than 3 then add this element with count
incremented by 1
map.put(element, map.get(element)+1);
}else
map.put(element, 1); // if element is not in map then add it it map with count 1
}
public void remove(String element){
if(map.containsKey(element)){// if element is in map
if(map.get(element) == 1) // if count is 1 then remove from map
map.remove(element);
else // else decrement count by 1
map.put(element, map.get(element)-1);
}
}
public boolean member(String element){
return map.containsKey(element);
}
public void print(){
// get each entry of map and print key and value
for(Map.Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
public class Max3BagTest {
public static void main(String[] args) {
Max3Bag A = new Max3Bag();
A.add("a");
A.add("a");
A.add("a");
A.add("a");
A.add("b");
A.add("b");
A.add("b");
A.remove("a");
A.add("c");
A.print();
}
}
/*
Sample Output:
a=2
b=3
c=1
*/

More Related Content

More from aptind

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
aptind
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
aptind
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
aptind
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
aptind
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
aptind
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
aptind
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
aptind
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
aptind
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
aptind
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
aptind
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
aptind
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
aptind
 
FALSSolutionFALS.pdf
FALSSolutionFALS.pdfFALSSolutionFALS.pdf
FALSSolutionFALS.pdf
aptind
 
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
aptind
 
1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf
aptind
 
#include iostream     Provides cout. #include cstdlib   .pdf
 #include iostream      Provides cout. #include cstdlib   .pdf #include iostream      Provides cout. #include cstdlib   .pdf
#include iostream     Provides cout. #include cstdlib   .pdf
aptind
 
ultravoilet exites electrons of matter more than .pdf
                     ultravoilet exites electrons of matter more than .pdf                     ultravoilet exites electrons of matter more than .pdf
ultravoilet exites electrons of matter more than .pdf
aptind
 
The difference in the HNMR is the number of signa.pdf
                     The difference in the HNMR is the number of signa.pdf                     The difference in the HNMR is the number of signa.pdf
The difference in the HNMR is the number of signa.pdf
aptind
 
The balanced Equations are as follows 2SO2(g) +.pdf
                     The balanced Equations are as follows  2SO2(g) +.pdf                     The balanced Equations are as follows  2SO2(g) +.pdf
The balanced Equations are as follows 2SO2(g) +.pdf
aptind
 

More from aptind (20)

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
FALSSolutionFALS.pdf
FALSSolutionFALS.pdfFALSSolutionFALS.pdf
FALSSolutionFALS.pdf
 
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
 
1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf
 
#include iostream     Provides cout. #include cstdlib   .pdf
 #include iostream      Provides cout. #include cstdlib   .pdf #include iostream      Provides cout. #include cstdlib   .pdf
#include iostream     Provides cout. #include cstdlib   .pdf
 
ultravoilet exites electrons of matter more than .pdf
                     ultravoilet exites electrons of matter more than .pdf                     ultravoilet exites electrons of matter more than .pdf
ultravoilet exites electrons of matter more than .pdf
 
The difference in the HNMR is the number of signa.pdf
                     The difference in the HNMR is the number of signa.pdf                     The difference in the HNMR is the number of signa.pdf
The difference in the HNMR is the number of signa.pdf
 
The balanced Equations are as follows 2SO2(g) +.pdf
                     The balanced Equations are as follows  2SO2(g) +.pdf                     The balanced Equations are as follows  2SO2(g) +.pdf
The balanced Equations are as follows 2SO2(g) +.pdf
 

Recently uploaded

Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 

Hi please find my code.import java.util.HashMap;import java.util.pdf

  • 1. Hi please find my code. import java.util.HashMap; import java.util.Map; public class Max3Bag { private HashMap map; public Max3Bag() { map = new HashMap<>(); } public void add(String element){ if(map.containsKey(element)){ // if map already contains this element if(map.get(element) < 3) // if count is less than 3 then add this element with count incremented by 1 map.put(element, map.get(element)+1); }else map.put(element, 1); // if element is not in map then add it it map with count 1 } public void remove(String element){ if(map.containsKey(element)){// if element is in map if(map.get(element) == 1) // if count is 1 then remove from map map.remove(element); else // else decrement count by 1 map.put(element, map.get(element)-1); } } public boolean member(String element){ return map.containsKey(element); } public void print(){ // get each entry of map and print key and value
  • 2. for(Map.Entry entry : map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } } } public class Max3BagTest { public static void main(String[] args) { Max3Bag A = new Max3Bag(); A.add("a"); A.add("a"); A.add("a"); A.add("a"); A.add("b"); A.add("b"); A.add("b"); A.remove("a"); A.add("c"); A.print(); } } /* Sample Output: a=2 b=3 c=1 */ Solution Hi please find my code.
  • 3. import java.util.HashMap; import java.util.Map; public class Max3Bag { private HashMap map; public Max3Bag() { map = new HashMap<>(); } public void add(String element){ if(map.containsKey(element)){ // if map already contains this element if(map.get(element) < 3) // if count is less than 3 then add this element with count incremented by 1 map.put(element, map.get(element)+1); }else map.put(element, 1); // if element is not in map then add it it map with count 1 } public void remove(String element){ if(map.containsKey(element)){// if element is in map if(map.get(element) == 1) // if count is 1 then remove from map map.remove(element); else // else decrement count by 1 map.put(element, map.get(element)-1); } } public boolean member(String element){ return map.containsKey(element); } public void print(){ // get each entry of map and print key and value for(Map.Entry entry : map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue());
  • 4. } } } public class Max3BagTest { public static void main(String[] args) { Max3Bag A = new Max3Bag(); A.add("a"); A.add("a"); A.add("a"); A.add("a"); A.add("b"); A.add("b"); A.add("b"); A.remove("a"); A.add("c"); A.print(); } } /* Sample Output: a=2 b=3 c=1 */