Map
- It is not consider as a collection, because it not implements the property of collection interface.
- Map is use to store key and value.
- Each key and value pair are called as entry.
- Key is always unique in Map and values can be duplicate.
- Map has multiple implemented classes
- HashMap
- Hashtable
- LinkedHashMap
- TreeMap
HashMap
- HashMap implements the property from Map interface.
- HashMap is also a collection of entries (key and values).
- HashMap keys must be unique and can be of difference data type.
- Values can be duplicate and can be of different data type.
- Map entries are dynamic in size.
- Based on hashing algo.
- HashMap entries are un-order and un-sorted.
- 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
- Hashtable implements the property from Map interface.
- Hashtable is also a collection of entries (key and values).
- Hashtable keys must be unique and can be of difference data type.
- Values can be duplicate and can be of different data type.
- Map entries are dynamic in size.
- Based on hashing algo.
- Hashtable entries are un-order and un-sorted.
- Hashtable cannot contains null key or null values.
- Hashtable methods are synchronized.
- Object of Hashtable is thread safe. Only one thread can access the object of Hashtable.
- 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
- LinkedHashMap extends the HashMap and implements the property from Map interface.
- LinkedHashMap is also a collection of entries (key and values).
- LinkedHashMap keys must be unique and can be of difference data type.
- Values can be duplicate and can be of different data type.
- Map entries are dynamic in size.
- Based on hashing algo and doubly linked list algo.
- LinkedHashMap entries are order (maintain an insertion order) and un-sorted.
- LinkedHashMap can contains one null key and multiple null values.
TreeMap
- TreeMap implements the property from Map, NavigableMap, SortedMap interface.
- TreeMap is a collection of entries (key and values).
- TreeMap keys must be unique and must be of same data type.
- Values can be duplicate and can be of different data type.
- Map entries are dynamic in size.
- TreeMap is based on balanced tree algo.
- TreeMap stores entries in sorted by key.
- 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
Job’s For Fresher