0% found this document useful (0 votes)
6 views1 page

Java HashMap Example and Usage

The document provides a Java code example demonstrating the use of a HashMap. It shows how to declare a HashMap, add elements, retrieve values based on keys, and remove elements. The code includes print statements to display the retrieved values and the HashMap's state after removal.

Uploaded by

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

Java HashMap Example and Usage

The document provides a Java code example demonstrating the use of a HashMap. It shows how to declare a HashMap, add elements, retrieve values based on keys, and remove elements. The code includes print statements to display the retrieved values and the HashMap's state after removal.

Uploaded by

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

# HashMapExample

import [Link].*;

public class HashMapExample {


public static void main(String args[]) {

/* This is how to declare HashMap */


HashMap<Integer, String> hmap = new HashMap<Integer, String>();

/*Adding elements to HashMap*/


[Link](12, "Chaitanya");
[Link](2, "Rahul");
[Link](7, "Singh");
[Link](49, "Ajeet");
[Link](3, "Anuj");

/* Get values based on key*/


String var= [Link](2);
[Link]("Value at index 2 is: "+var);
String var2= [Link](7);
[Link]("Value at index 7 is: "+var2);

/* Remove values based on key*/


[Link](3);
[Link](hmap);

}
}

You might also like