Alphalearningschool
        

<<< Prev

Next >>>

Up
Java Map

MAP:

Prerequisite: Idea of collection framework, both are different but if you know collection then it will be  easy to understand.

Introduction:

  • Map is an interface present in java.util.package.
  • Map is not a child interface of collection.
  • Map has two types of argument one as key and other as

For further understanding, we will look block diagram of MAP.

 

MAP Interface:

  • The map type object contains values in form of Key and Value pair.
  • Map cannot contain duplicate keys, value can be duplicate but only once.
  • In map, each key should shave only one value. 

MAP Architecture:

  • In above table we are assigning values to their respective keys and it doesn’t allow any duplicate key which is marked as red.
  • Collection of keys in the above table is called as set view.
  • Collection of values in the above table is called as List view.
  • And table itself is called as Map view.

Function of MAP (Basic Operation/Method):

 

Tree Map Class:

  • Basic:
  1. Tree Map contains values based on key.
  2. It is extended by AbstractMap class.
  3. It contains only unique elements.
  4. Key cannot be null but values can be null (Multiple value also).
  5. TreeMap implements Map and SortedMap and maintains ascending order.

Let us understand TreeMap with example:

  1. How to declare TreeMap class .
  • Public class TreeMap<K,V>
  • K for Key and V for Value.
Ex:
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Demo {
public static void main(String[] args) {
TreeMap<Integer, String> tm = new TreeMap<>();
tm.put(101, "Roger");
tm.put(102, "Alias");
tm.put(103, "Epsum");

// Iterate Elements using For Each Loop
for (Map.Entry s : tm.entrySet()) {
System.out.println(s.getKey() + " Key " + "Value " + s.getValue());
}

System.out.println();

// Iterate Element using Iterator
Iterator<Map.Entry<Integer, String>> it = tm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + " Key " + " Value " + entry.getValue());
}
}

}



Output:
101 Key Value Roger
102 Key Value Alias
103 Key Value Epsum

101 Key  Value Roger
102 Key  Value Alias
103 Key  Value Epsum

 

HashMap:

Basic:

  • HashMap and TreeMap both are same.
  • It extends AbstractMap class.
  • In Hash Map allow only one Null key and multiple Null value.
  • HashMap does not maintain any order. 

Let us understand HashMap with example:

  1. How to declare HashMap class .
  • Public class HashMap<K,V>
  • K for Key and V for Value
Ex:
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

public class Demo {
public static void main(String[] args) {
Map<Integer, String> tm = new HashMap<>();
tm.put(101, "Roger");
tm.put(102, "Alias");
tm.put(103, "Epsum");

// Iterate Elements using For Each Loop
for (Map.Entry s : tm.entrySet()) {
System.out.println(s.getKey() + " Key " + "Value " + s.getValue());
}

System.out.println();

// Iterate Element using Iterator
Iterator<Map.Entry<Integer, String>> it = tm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + " Key " + " Value " + entry.getValue());
}
}

}

Output:
101 Key Value Roger
102 Key Value Alias
103 Key Value Epsum

101 Key  Value Roger
102 Key  Value Alias
103 Key  Value Epsum

 

LinkedHashMap:

Basic:

  • LinkedHashMap and HashMap both are same.
  • It extends HashMap class and implemented by NavigableMap.
  • In LinkedHashMap, it allows only one Null key and multiple Null values.
  • LinkedHashMap maintains insertion order. 

Let us understand LinkedHashMap with example:

  1. How to declare LinkedHashMap class .
  • Public class LinkedHashMap<K,V>
  • K for Key and V for Value
Ex:
import java.util.Iterator;
import java.util.Map;
import java.util.LinkedHashMap;

public class Demo {
public static void main(String[] args) {
Map<Integer, String> tm = new LinkedHashMap<>();
tm.put(101, "Roger");
tm.put(102, "Alias");
tm.put(103, "Epsum");

// Iterate Elements using For Each Loop
for (Map.Entry s : tm.entrySet()) {
System.out.println(s.getKey() + " Key " + "Value " + s.getValue());
}

System.out.println();

// Iterate Element using Iterator
Iterator<Map.Entry<Integer, String>> it = tm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + " Key " + " Value " + entry.getValue());
}
}

}



Output:
101 Key Value Roger
102 Key Value Alias
103 Key Value Epsum

101 Key  Value Roger
102 Key  Value Alias
103 Key  Value Epsum

Hashtable:

Basic:

  • Hashtable implements MAP interface and extends Dictionary class.
  • Hashtable contain only unique elements.
  • Hashtable doesn’t allow null key or value.
  • Hashtable is synchronized and thread safe.
  • Hashtable is high cost and time consuming because of thread safe. 

Let us understand LinkedHashMap with example:

  1. How to declare Hashtable class.
  • Public class Hashtable<K,V>
  • K for Key and V for Value
Ex:
import java.util.Iterator;
import java.util.Map;
import java.util.Hashtable;

public class Demo {
public static void main(String[] args) {
Map<Integer, String> tm = new Hashtable<>();
tm.put(101, "Roger");
tm.put(102, "Alias");
tm.put(103, "Epsum");

// Iterate Elements using For Each Loop
for (Map.Entry s : tm.entrySet()) {
System.out.println(s.getKey() + " Key " + "Value " + s.getValue());
}

System.out.println();

// Iterate Element using Iterator
Iterator<Map.Entry<Integer, String>> it = tm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + " Key " + " Value " + entry.getValue());
}
}

}



Output:
101 Key Value Roger
102 Key Value Alias
103 Key Value Epsum

101 Key  Value Roger
102 Key  Value Alias
103 Key  Value Epsum

 

Let us we discussed what difference between HashMap and Hashtable. 

HashMap

Hashtable

HashMap is Not synchronized.

It is synchronized

HashMap is not thread safe.

Hashtable is thread safe

Fast and less time consuming.

Slow and time consuming

HashMap allow one null key and multiple null values.

Hashtable not allow any null key and values.

It’s introduced in jdk 1.2 onwards

This is a legacy class.

HashMap is traversed by Iterator.

Hashtable traversed by Enumerator and Iterator.

Iterator in HashMap is Fail-Fast

Enumerator in Hashtable is not fail- fast.

HashMap extends by AbstractMap class.

Hashtable extends  dictionary class.

Let us do one more example with using all method: 

Ex:
package com.TreeMap.Tutorial;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Demo {
public static void main(String[] args) {
//Map<Integer, String> tm = new TreeMap<>();

//Map<Integer, String> tm = new HashMap<>();

//Map<Integer, String> tm = new LinkedHashMap<>();

Map<Integer, String> tm = new Hashtable<>();

tm.put(101, "Roger");
tm.put(102, "Alias");
tm.put(103, "Epsum");

System.out.println("Actual output of Map object : " + tm);
tm.putAll(tm);
System.out.println();
System.out.println("Object as argument : " + tm);
String t = tm.get(101);
System.out.println();
System.out.println("Give value of Key :- " + t);
boolean t1 = tm.containsKey(102);
System.out.println();
System.out.println("Give Output key Lies in Map or not :- " + t1);
System.out.println();
System.out.println("Give set of key in Map :- " + tm.keySet());
System.out.println();
System.out.println("Give set of key and value :- " + tm.entrySet());
System.out.println();
tm.remove(103);
System.out.println("Remove key thne after Output : " + tm);

}

}



Output:

Actual output of Map object : {103=Epsum, 102=Alias, 101=Roger}

Object as argument : {103=Epsum, 102=Alias, 101=Roger}

Give value of Key :- Roger

Give Output key Lies in Map or not :- true

Give set of key in Map :- [103, 102, 101]

Give set of key and value :- [103=Epsum, 102=Alias, 101=Roger]

Remove key thne after Output : {102=Alias, 101=Roger}