Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in java show proper comments on each line of code This assignment will help

ID: 3867872 • Letter: C

Question

Code in java show proper comments on each line of code



This assignment will help you to master the following concepts: HashMap Comparator 3. Prerequisites Not applicable. 4. Associated Data Files Not applicable. 5. Problem Statement Create a HashMap object in the Java Program and perform the following functionalities Add key value pairs to the HashMap object Add a key-value pair to the HashMap object if the key-value pair doesn't exist already. Retrieve a value associated with a given key from the HashMap. Clear all the key-value pair present in the HashMap.

Explanation / Answer

HashMapDemo.java

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {

public static void main(String[] args) {

/*
* Creating a HashMap Object and insert some key value pairs in it
*/
HashMap < String, String > m = new HashMap < String, String > ();
m.put("Marty", "Stepp");
m.put("Stuart", "Reges");
m.put("Jessica", "Miller");
m.put("Amanda", "Camp");
m.put("Hal", "Perkins");

//Synchronizing the HashMap Explicitly
Map map = Collections.synchronizedMap(m);
synchronized(m) {
Set < String > kys = m.keySet();
//Displaying the Keys
System.out.println(" Displaying the keys :");
for (String key: kys) {
System.out.println(key);
}

Set st = m.entrySet();
//Creating an Iterator Object
Iterator itr = st.iterator();
//Displaying the values
System.out.println("Displaying the values :");
while (itr.hasNext()) {
Map.Entry mp = (Map.Entry) itr.next();
System.out.println(mp.getValue());
}

}

}
}

____________________

Output:


Displaying the keys :
Hal
Marty
Stuart
Amanda
Jessica
Displaying the values :
Perkins
Stepp
Reges
Camp
Miller

_______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote