Code in java show proper comments on each line of code
show output on Eclipse console
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.
Expert Answer
package zasda;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {
public void mapadd(int key , String value , HashMap hmap){
if (hmap.containsKey(key) || hmap.containsValue(value)){
hmap.put(key, value);
}
}
public static void main(String args[]) {
/* This is how to declare HashMap */
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
/*Add key value pair to hash map*/
hmap.put(12, “Sims”);// adding key and a value in hmap
hmap.put(15, “agha”);// enter the respective value of key and object to store in hmap
hmap.get(12); // enter the key value of the object to be fetched
hmap.clear();// used to clear all the values in hmap
}
}