Question

In: Computer Science

HashMap is a Map based collection class that is used for storing Key & value pairs,...

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.

Solutions

Expert Solution

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());
}
   }
}


Related Solutions

public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
Analyze the pros and cons of cloud-based technologies which are used for storing data at the...
Analyze the pros and cons of cloud-based technologies which are used for storing data at the personal, corporate, and governmental level.  
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value)....
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value). addToMultiMap must add the value, if present, to the set associated with the given key, creating the set if necessary. You may assume all keys already in the map are associated with non-null values. For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map,...
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of different elements. Please design and implement your own array-based version of MyArrayList that stores items of type String and supports the following operations: +MyArrayList() //a constructor that creates an empty array list +emptyHere() : Boolean // Determines whether the list is empty or not +sizeHere() : integer // Returns the # of items stored in the array list +add(in item : String) : void...
what are some of the key diffrences between compatancy based and value based interviews
what are some of the key diffrences between compatancy based and value based interviews
Question 1 The annotation that is used to identify the primary key attribute of a class....
Question 1 The annotation that is used to identify the primary key attribute of a class. Group of answer choices Id Column Entity OneToMany Question 2 The entity manager method that selects a row from a table given a primary key. Group of answer choices getEntity close find persist Question 3 The annotation that is used to identify the tale that the class is mapped to. Group of answer choices Temporal Id Table Entity Question 4 The annotation that specifies...
Based on the notations we’ve learned in class and being used in this exam, which of...
Based on the notations we’ve learned in class and being used in this exam, which of the following genotypes is a correct way to write out an individual having Wilson’s disease and red green color blindness? SELECT ALL ATP7B/atp7b XAXa ATP7B/atp7b XAY atp7b /atp7b XaY atp7b /atp7b XaXa
Find a poll online or paper-based that has not been used in the class. Analyze and...
Find a poll online or paper-based that has not been used in the class. Analyze and evaluate the poll using the 4 critical questions to determine whether the poll is problematic or not.
Describe the key logistics processes and supply chain functions used to create value for Tesco’s global...
Describe the key logistics processes and supply chain functions used to create value for Tesco’s global supply chain.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT