• Register
100 points
5 1

HashMap

HashMap Maybe define as the Collection class based on hashing technique used for storing data in the form of key and value pairs.It is denoted by the syntax. HashMap<K,V>, where K is the key and V is the value based on the key.It is similar to Hash Table but is allow single null key and multiple null values and  unsynchronized. HashMap is an unordered collection because it does not return the keys and values in the same order in which they have been inserted into the HashMap.To use the HashMap and its method programmer must import  java.util.HashMap or its super class.
 

Code

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {

   public static void main(String args[]) {

      /* This is how to declare HashMap */
      HashMap<String, String> hmap = new HashMap<String, String>();

      /*Adding elements to HashMap*/
      hmap.put("John", "First");
      hmap.put("Sam", "Second");
      hmap.put("Robert", "Third");
      hmap.put("Rita", "Forth");
      hmap.put("Alex", "Fifth");

      /* Display content using Iterator*/
      Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
        
         System.out.println("key: "+ mentry.getKey() + " & Value: "+mentry.getValue());
      }

      /* Get values based on key*/
      String var= hmap.get("Alex");
      System.out.println("Value at index Alex is: "+var);

      /* Remove values based on key*/
      hmap.remove("Sam");
      System.out.println("Map key and values after removal:");
      Set set2 = hmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.println("Key: "+mentry2.getKey() + " & Value: "+mentry2.getValue());
       }

   }
}

Output

key: Alex & Value: Fifth
key: Rita & Value: Forth
key: Robert & Value: Third
key: John & Value: First
key: Sam & Value: Second
Value at index Alex is: Fifth
Map key and values after removal:
Key: Alex & Value: Fifth
Key: Rita & Value: Forth
Key: Robert & Value: Third
Key: John & Value: First

LinkedHashMap

LinkedHaashMap may be define as the collection class which is a combination of Hash table and linked list. It is the extended version HashMap in which linked list is used instead of array.It is denoted by the syntax LinkedHashMap<K,V>, where K is the key and V is the value based on the key.It is similar to HashMap instead maintains insertion order.To use the LinkedHashMap and its method programmer must import  java.util.LinkedHashMap or its super class.
 

Code

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
 class Details {

   public static void main(String args[]) {

      /* This is how to declare LinkedHashMap */
      LinkedHashMap<String, String> lhmap = new LinkedHashMap<String, String>();

      /*Adding elements to LinkedHashMap*/
      lhmap.put("John", "First");
      lhmap.put("Sam", "Second");
      lhmap.put("Robert", "Third");
      lhmap.put("Rita", "Forth");
      lhmap.put("Alex", "Fifth");

      /* Display content using Iterator*/
      Set set = lhmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
        
         System.out.println("key: "+ mentry.getKey() + " & Value: "+mentry.getValue());
      }

      /* Get values based on key*/
      String var= lhmap.get("Alex");
      System.out.println("Value at index Alex is: "+var);

      /* Remove values based on key*/
      lhmap.remove("Sam");
      System.out.println("Map key and values after removal:");
      Set set2 = lhmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.println("Key: "+mentry2.getKey() + " & Value: "+mentry2.getValue());
       }

   }
}

Output

key: John & Value: First
key: Sam & Value: Second
key: Robert & Value: Third
key: Rita & Value: Forth
key: Alex & Value: Fifth
Value at index Alex is: Fifth
Map key and values after removal:
Key: John & Value: First
Key: Robert & Value: Third
Key: Rita & Value: Forth
Key: Alex & Value: Fifth

TreeMap

TreeMap may be define as a data-visualization technique for large amounts of hierarchically structured (tree-structured) data based on Red-Black tree.It stores data using nested rectangles.It The Data is sorted according to natural ordering by using comparison mechanism but is is also unsynchronized.
It extends AbtractMap class and implements the NavigableMap interface.It continously unique key and value.It is denoted by the syntax TreeMap<K,V> for corresponding key and value.To use the TreeMap and its method programmer must import  java.util.TreeMap or its super class.

Code

import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
 class Details {

   public static void main(String args[]) {

      /* This is how to declare TreeMap */
      TreeMap<String, String> tmap = new TreeMap<String, String>();

      /*Adding elements to TreeMap*/
      tmap.put("John", "First");
      tmap.put("Sam", "Second");
      tmap.put("Robert", "Third");
      tmap.put("Rita", "Forth");
      tmap.put("Alex", "Fifth");

      /* Display content using Iterator*/
      Set set = tmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
        
         System.out.println("key: "+ mentry.getKey() + " & Value: "+mentry.getValue());
      }

      /* Get values based on key*/
      String var= tmap.get("Alex");
      System.out.println("Value at index Alex is: "+var);

      /* Remove values based on key*/
      tmap.remove("Sam");
      System.out.println("Map key and values after removal:");
      Set set2 = tmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.println("Key: "+mentry2.getKey() + " & Value: "+mentry2.getValue());
       }

   }
}

Output

key: Alex & Value: Fifth
key: John & Value: First
key: Rita & Value: Forth
key: Robert & Value: Third
key: Sam & Value: Second
Value at index Alex is: Fifth
Map key and values after removal:
Key: Alex & Value: Fifth
Key: John & Value: First
Key: Rita & Value: Forth
Key: Robert & Value: Third