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

Java HashMap Basics and Operations

The document provides a Java program demonstrating the use of HashMap, including creation, insertion, searching, iteration, and removal of key-value pairs. It shows how to add entries, check for the existence of keys, retrieve values, and iterate through the map. The program also includes examples of updating values and removing entries from the HashMap.

Uploaded by

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

Java HashMap Basics and Operations

The document provides a Java program demonstrating the use of HashMap, including creation, insertion, searching, iteration, and removal of key-value pairs. It shows how to add entries, check for the existence of keys, retrieve values, and iterate through the map. The program also includes examples of updating values and removing entries from the HashMap.

Uploaded by

Roshan K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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