Question & Answer: 4. Associated Data Files Not applicable 5. Problem Statement .Create a HashMap object in the Java Program and perform the following functionalities…..

Write the program in java and make proper comments.

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 Retrieve all the keys present in the HashMap. Retrieve all the values present in the HashMap Make the HashMap as Synchronized. 6. Expected Output Files to be submitted for verification

media%2Ff14%2Ff143168e-a43d-492e-ae87-fc

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 Retrieve all the keys present in the HashMap. Retrieve all the values present in the HashMap Make the HashMap as Synchronized. 6. Expected Output Files to be submitted for verification

Expert Answer

 

Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

___________________

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(“nDisplaying 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

Still stressed from student homework?
Get quality assistance from academic writers!