logo

What is HashMap in Java and how to implement this?

CATEGORY : Technology AUTHOR : BOOK BANK

HashMap is a part of Java’s collection since Java 1.2. This class is found in java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs, and you can access them by an index of another type (e.g. an Integer). One object is used as a key (index) to another object (value). If you try to insert the duplicate key, it will replace the element of the corresponding key.

HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.  This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass.

public class HashMapexample {
    public static void main(String[] args){
    HashMapexample hmp = new HashMapexample();
    hmp.HmExample();
    }
    public String HmExample(){
    String result = "";
    HashMap map = new HashMap<>();
    map.put("Anand",30);
    map.put("Atul",40);
    map.put("Anand",50);
    map.put("Atul",60);
    System.out.println("Side of map is ="+map.size());
    System.out.println("Map values are ="+map);
    System.out.println("Map values for anand is ="+map.get("Anand"));
    return result;
    }
}

Output for HmExample(): 

Side of map is =2
Map values are ={Atul=60, Anand=50}
Map values for anand is =50

///////////////////////////////////////////////////////////

public class HashMapexample {
    public static void main(String[] args){
    HashMapexample hmp = new HashMapexample();
    hmp.HmExample();
    hmp.HashamapExampleByIntStr();
    }
    public String HmExample(){
    String result = "";
    HashMap<String, Integer> map = new HashMap<>();
    map.put("Anand",30);
    map.put("Atul",40);
    map.put("Anand",50);
    map.put("Atul",60);
    System.out.println("Side of map is ="+map.size());
    System.out.println("Map values are ="+map);
    System.out.println("Map values for anand is ="+map.get("Anand"));
    return result;
    }
    public String HashamapExampleByIntStr(){
       String result  = "";
       HashMap<Integer, String> map = new HashMap<>();
       map.put(20, "Anand");
       map.put(50, "Nicky");
       map.put(90, "Atul");
       System.out.println("Hashmap lists are"+map);
        System.out.println("Hashmap lists Size"+map.size());
         System.out.println("Map value for Nicky is = "+map.get("Nicky"));
       return result;
    }
}
 

 

Output for HashamapExampleByIntStr:

Side of map is =2
Map values are ={Atul=60, Anand=50}
Map values for anand is =50
Hashmap lists are{50=Nicky, 20=Anand, 90=Atul}
Hashmap lists Size3
Map value for Nicky is = null

 

 

Customer Reviews

  • themes

    Price
    Value
    Quality

    themes

    Review byplaza

    Posted on 12/9/16

You're reviewing:

Joust Duffle Bag

Your Rating *
Value
Quality
Price

Related Books