Saturday, January 11, 2020

HashMap in Java

HashMap in Java


HashMap implements the Map interface and belongs to the Collections framework in Java. HashMap allows us to store values associated with the keys or the information is stored in HashMap in key-value pairs. A Map cannot contain duplicate keys. You can get the object from a Map using a key. HashMap provides no guarantee of ordering. HashMap allows null key and null values. HashMap offers constant time access for a general method to get, put, and contain values. An object of Map represents a group of objects, each of which is associated with a key.

HashMaps in Java

HashMap Example

/******HASHMAP EXAMPLE******/
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
class Car
      int price;
      String brand;
      String color;
      int model_year;
      public Car(int price, String brand, String color, int model_year) {
            this.price = price;
            this.brand = brand;
            this.color = color;
            this.model_year = model_year;
public class HashMapDemo {   
public static void main(String[] args) {   
 TreeMap<Integer, Car> hm=new TreeMap<>();
 hm.put(2, new Car(300000,"Maruti","White",2015));
 hm.put(4, new Car(300000,"Toyota","White",2016));
 hm.put(12, new Car(350000,"Maruti","White",2015));
 hm.put(7, new Car(450000,"Hyundai","Red",2017));
 hm.put(19, new Car(200000,"Honda","Red",2017));
 hm.put(78, null);
      for(Entry<Integer, Car> e:hm.entrySet())
            System.out.println("Key: "+e.getKey()+" Car : price :"+ e.getValue().price+" Brand:"+e.getValue().brand);
 }catch (Exception e) {
System.out.println("Exception null value");

Key: 2 Car : price :300000 Brand:Maruti
Key: 4 Car : price :300000 Brand:Toyota
Key: 7 Car : price :450000 Brand:Hyundai
Key: 12 Car : price :350000 Brand:Maruti
Key: 19 Car : price :200000 Brand:Honda
Exception null value

HashMap Methods

public boolean containsKey(Object key)

This method returns true if this map contains a mapping for the defined key.

public void clear()

This method removes all of the mappings from this map. The map will be empty after this call returns.

public boolean containsKey(Object key)

This method returns true if this map contains a mapping for the defined key.

public boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the defined value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations.

public Set<Map.Entry<K,V>> entrySet()

This method returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order. The set's spliterator is late-binding, fail-fast, and additionally reports Spliterator.SORTED and Spliterator.ORDERED with an encounter order that is ascending key order.

public V get(Object key)

This method returns the value to which the defined key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that key compares equal to k according to the map's ordering, then this method returns v; otherwise, it returns null. (There can be at most one such mapping.)

This method returns a value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

public V put(K key, V value)

This method associates the defined value with the defined key in this map. If the map previously contained a mapping for the key, the old value is replaced.

public void putAll(Map<? extends K,? extends V> map)

This method copies all of the mappings from the defined map to this map. These mappings replace any mappings that this map had for any of the keys currently in the defined map.

Video Tutorial