In: Computer Science
HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).
5.Write a Java program to test if a map contains a mapping for the specified key.a.Add values in the Hash mapb.Printthe mapc.Check for a specified key, print result
6.Write a Java program to copy all of the mappings from the specified map to another map. Use iterator with both for each and whileloop,print out the key with the matching value.
You can combine them into one single program.
5 ).
Code:
package demo;
import java.util.HashMap;
import java.util.Scanner;
public class HashMap1 {
public static void main(String[] args) {
// create a hashmap
HashMap<Integer,String>
map=new HashMap<Integer,String>();
//
create a scanner object
Scanner in = new
Scanner(System.in);
// put values into the map like
student roll number and name
map.put(1,"Arjun"); //Put elements
in Map
map.put(2,"Ravi");
map.put(3,"Kiran");
map.put(4,"Suresh");
map.put(5, "Tarun");
// print the map
System.out.println(map);
// read the search key from the
user
System.out.println("Enter the key
to Search");
int key = in.nextInt();
// check if the entered key is
present or not
if(map.containsKey(key)){
System.out.println("The hashmap contains key " + key+"
value is " + map.get(key));
}
// if the key is not present
else {
System.out.println("The hashmap does not contains key
"+key);
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
6 ).
Code:
package demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class HashMap2 {
public static void main(String[] args) {
// create two hashmaps map1 and
map2
HashMap<Integer,String>
map1=new HashMap<Integer,String>();//Creating HashMap
HashMap<Integer,String>
map2=new HashMap<Integer,String>();
// insert elements into the hash map
for example key is roll number and name is value
map1.put(1,"Arjun");
map1.put(2,"Ravi");
map1.put(3,"Kiran");
map1.put(4,"Suresh");
map1.put(5, "Tarun");
// insert some student names and roll numbers to
map2
map2.put(6,"Karna"); //Put elements in Map
map2.put(7,"Narayana");
map2.put(8,"Surya");
map2.put(9,"Shiva");
map2.put(10, "Kanish");
// copy all the map1 elements to map2
map2.putAll(map1);
System.out.println("...................ForEach...........................");
// iteration using for each through the map2
map2.forEach((k,v) -> System.out.println("Key - "+
k + " & " +"Value - "+ v));
System.out.println(".................Using while
loop.........................");
// using while loop to print hash map
// create an object for iterator and use it to print
hashmap
Iterator iterator = map2.entrySet().iterator();
// check if the hash map contains next element
while (iterator.hasNext()) {
//
Map.Entry entry = (Map.Entry) iterator.next();
// print the key and value
System.out.println("Key - "+entry.getKey()+" & Value - "+
entry.getValue());
}
}
}