Skip to content
Home » Java Programing Language » Collection In Java » Map In Java

Map In Java

Map

  1. It is not consider as a collection, because it not implements the property of collection interface.
  2. Map is use to store key and value.
  3. Each key and value pair are called as entry.
  4. Key is always unique in Map and values can be duplicate.
  5. Map has multiple implemented classes
    1. HashMap
    2. Hashtable
    3. LinkedHashMap
    4. TreeMap

 

HashMap

  1. HashMap implements the property from Map interface.
  2. HashMap is also a collection of entries (key and values).
  3. HashMap keys must be unique and can be of difference data type.
  4. Values can be duplicate and can be of different data type.
  5. Map entries are dynamic in size.
  6. Based on hashing algo.
  7. HashMap entries are un-order and un-sorted.
  8. HashMap can contains one null key and multiple null values.

Example:-

import java.util.LinkedHashMap;

public class HashMapDemo {
	public static void main(String[] args) {
		LinkedHashMap map = new LinkedHashMap();
		map.put(123, "kalyan");
		map.put(123, 657.987);
		map.put("Sushil", "Bhagde");
		map.put('A', 654);
		map.put(null, 4444);
		map.put(000, null);
		map.put(123, "kalyan");
		System.out.println(map);
		
		System.out.println("Keys : "+ map.keySet());
		System.out.println("Values : "+ map.values());
		
		System.out.println(map.get(123));
		
		map.remove("kalyan");
		
		System.out.println(map);
		
		System.out.println(map.containsKey(123));
		System.out.println(map.containsValue(7777));
	}
}

 

Hashtable

  1. Hashtable implements the property from Map interface.
  2. Hashtable is also a collection of entries (key and values).
  3. Hashtable keys must be unique and can be of difference data type.
  4. Values can be duplicate and can be of different data type.
  5. Map entries are dynamic in size.
  6. Based on hashing algo.
  7. Hashtable entries are un-order and un-sorted.
  8. Hashtable cannot contains null key or null values.
  9. Hashtable methods are synchronized.
  10. Object of Hashtable is thread safe. Only one thread can access the object of Hashtable.
  11. Hashtable is slower in the performance than HashMap.

Example:-

import java.util.Hashtable;

public class HashTableDemo {
	public static void main(String[] args) {
		Hashtable map = new Hashtable();
		map.put(123, "kalyan");
		map.put(123, 657.987);
		map.put("Sushil", "Bhagde");
		map.put('A', 654);
		//map.put(null, 4444);
		//map.put(000, null);
		map.put(123, "kalyan");
		System.out.println(map);	
	}
}

 

LinkedHashMap

  1. LinkedHashMap extends the HashMap and implements the property from Map interface.
  2. LinkedHashMap is also a collection of entries (key and values).
  3. LinkedHashMap keys must be unique and can be of difference data type.
  4. Values can be duplicate and can be of different data type.
  5. Map entries are dynamic in size.
  6. Based on hashing algo and doubly linked list algo.
  7. LinkedHashMap entries are order (maintain an insertion order) and un-sorted.
  8. LinkedHashMap can contains one null key and multiple null values.

 

TreeMap

  1. TreeMap implements the property from Map, NavigableMap, SortedMap interface.
  2. TreeMap is a collection of entries (key and values).
  3. TreeMap keys must be unique and must be of same data type.
  4. Values can be duplicate and can be of different data type.
  5. Map entries are dynamic in size.
  6. TreeMap is based on balanced tree algo.
  7. TreeMap stores entries in sorted by key.
  8. Null keys are not allowed in TreeMap but can add null values.

 

Example :-

import java.util.LinkedHashMap;
import java.util.TreeMap;
import java.util.Map.Entry;

public class TreeMapDemo {

	public static void main(String[] args) {
		TreeMap map = new TreeMap();
		map.put(123, "Abc");
		map.put(345, 930.9);
		map.put(122, "Pqr");
		map.put(123, "kalyan");
		map.put(123, 657.987);
		map.put(000, null);
		map.put(123, "kalyan");
		System.out.println(map);
		
		System.out.println("First Entry:  "+map.firstEntry());
		Entry firstEntry = map.firstEntry();
		
		System.out.println("First key : "+ firstEntry.getKey() + " First value : "+ firstEntry.getValue());
		
		Entry lastEntry = map.lastEntry();
		System.out.println("Last Entry : "+lastEntry);
		System.out.println("Last key : "+ lastEntry.getKey() + " Last Value : "+lastEntry.getValue());
		System.out.println("First Key : "+ map.firstKey());
		System.out.println("Last Key: "+ map.lastKey());
		System.out.println("Higher : "+ map.higherKey(122));
		System.out.println("Lower : "+ map.lowerKey(122));
		System.out.println("Ceiling : "+ map.ceilingKey(122));
		System.out.println("Floor : "+ map.floorKey(122));
	}
}

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !