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

Code 2

The document presents a Java program that demonstrates the use of a HashMap to store and manage key-value pairs. It initializes a HashMap with integer keys and string values, overwrites a value for a specific key, and prints all entries along with the value associated with key 2. The program showcases basic operations such as adding, retrieving, and iterating over elements in a HashMap.

Uploaded by

Laxmi Pandey
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)
6 views1 page

Code 2

The document presents a Java program that demonstrates the use of a HashMap to store and manage key-value pairs. It initializes a HashMap with integer keys and string values, overwrites a value for a specific key, and prints all entries along with the value associated with key 2. The program showcases basic operations such as adding, retrieving, and iterating over elements in a HashMap.

Uploaded by

Laxmi Pandey
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

import [Link].

*;

public class HashMapExample {


public static void main(String[] args) {

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

[Link](1, "Tony");
[Link](2, "James");
[Link](3, "Tyrion");
[Link](2, "Lannister"); // overwrites value for key 2

[Link]("HashMap elements:");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]([Link]() + " = " +
[Link]());
}

// Get value by key


[Link]("Value for key 2: " + [Link](2));
}
}

You might also like