A HashMap in Java is a part of the Java Collection Framework and is used to store data in key-value pairs. It allows null values and the null key. It is not synchronized and is not ordered, meaning the order of the map is not guaranteed.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
System.out.println(map);
}
}
Console Output:
{Apple=1, Banana=2}
The put() method is used to add elements to the HashMap. If the key already exists, the new value overwrites the old value.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("Key1", "Value1");
map.put("Key2", "Value2");
map.put("Key1", "Value3"); // Overwrites Value1
System.out.println(map);
}
}
Console Output:
{Key1=Value3, Key2=Value2}
The remove() method removes the mapping for a key from this map if it is present.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.remove("One");
System.out.println(map);
}
}
Console Output:
{Two=2}
HashMap can be iterated using the for-each loop along with the entrySet() method, which returns a set view of the mappings contained in this map.
import java.util.HashMap;
import java.util.Map;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("A", "Apple");
map.put("B", "Banana");
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Console Output:
A = Apple
B = Banana
The isEmpty() method returns true if the map contains no key-value mappings.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
System.out.println("Is map empty? " + map.isEmpty());
map.put("X", 10);
System.out.println("Is map empty? " + map.isEmpty());
}
}
Console Output:
Is map empty? true
Is map empty? false
The size() method returns the number of key-value mappings in the map.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("C", "Cat");
map.put("D", "Dog");
System.out.println("Size of map: " + map.size());
}
}
Console Output:
Size of map: 2
The replace() method replaces the entry for a specified key only if it is currently mapped to some value.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("E", "Elephant");
map.replace("E", "Eagle");
System.out.println(map);
}
}
Console Output:
{E=Eagle}
The containsKey() method returns true if this map contains a mapping for the specified key.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map = new HashMap<>();
map.put("F", 6);
System.out.println("Contains key 'F'? " + map.containsKey("F"));
System.out.println("Contains key 'G'? " + map.containsKey("G"));
}
}
Console Output:
Contains key 'F'? true
Contains key 'G'? false
The clone() method is used to create a shallow copy of the HashMap instance.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map1 = new HashMap<>();
map1.put("G", "Giraffe");
HashMap map2 = (HashMap) map1.clone();
System.out.println("Original: " + map1);
System.out.println("Cloned: " + map2);
}
}
Console Output:
Original: {G=Giraffe}
Cloned: {G=Giraffe}
The putAll() method copies all of the mappings from the specified map to this map.
import java.util.HashMap;
class Example {
public static void main(String args[]) {
HashMap map1 = new HashMap<>();
map1.put("H", "Horse");
HashMap map2 = new HashMap<>();
map2.put("I", "Iguana");
map1.putAll(map2);
System.out.println(map1);
}
}
Console Output:
{H=Horse, I=Iguana}
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies