0% found this document useful (0 votes)
2 views2 pages

HashMap in Java

The document provides a Java program demonstrating the use of a HashMap, including creation, insertion, searching, iteration, and removal of key-value pairs. It shows how to add entries, update values, check for the presence of keys, and iterate through the map. The program also includes examples of printing the map's contents at various stages.

Uploaded by

aaamna.zahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

HashMap in Java

The document provides a Java program demonstrating the use of a HashMap, including creation, insertion, searching, iteration, and removal of key-value pairs. It shows how to add entries, update values, check for the presence of keys, and iterate through the map. The program also includes examples of printing the map's contents at various stages.

Uploaded by

aaamna.zahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HashMap in Java

import [Link].*;

public class Hashing {


public static void main(String args[]) {
//Creation
HashMap<String, Integer> map = new HashMap<>();

//Insertion
[Link]("India", 120);
[Link]("US", 30);
[Link]("China", 150);

[Link](map);

[Link]("China", 180);
[Link](map);

//Searching
if([Link]("Indonesia")) {
[Link]("key is present in the map");
} else {
[Link]("key is not present in the map");
}

[Link]([Link]("China")); //key exists


[Link]([Link]("Indonesia")); //key doesn't exist

//Iteration (1)
for( [Link]<String, Integer> e : [Link]()) {
[Link]([Link]());
[Link]([Link]());
}

//Iteration (2)
Set<String> keys = [Link]();
for(String key : keys) {
[Link](key+ " " + [Link](key));
}
//Removing
[Link]("China");
[Link](map);

}
}

You might also like