HashMap in java
Hi all, in this demo i will describe about HashMap in java.
Java HashMap is a hash table based implementation of Java’s Map interface.
A Map, is a collection of key-value pairs. It maps keys to values.
Map Interface.
public interface Map
An object that maps key to values. A map cannot contain duplicate keys;each key can map to at most one value.
This interface takes the place of the Disctionary class, which was a totally Abstract Class rather than an interface.
HashMap is important for selenium as well as for interview also.
hashMap is a Map based collection class that is used for storing Key & Value(associated with keys).
1).It is donated as HashMap
2).Hashmap contains only unique elements.
3).Hashmap may have only one null key and multiple null values
4). Hashmap maintains no order.
Here is the example of HashMap in Java.
import java.util.Map;
public class TestCsc2
{
public static void main(String args[])
{
Map<String, Integer> hmp = new HashMap<String, Integer>();
hmp.put(null, 6); //hash map can have one null key which will be stored at hashMap(0)
hmp.put("aaa", 1);
hmp.put("aaa", 5);
hmp.put("bbb", 2);
hmp.put("ccc", 3);
hmp.put("ddd", 4);
System.out.println(hmp);
}
}
Output :
{null=6, aaa=5, ccc=3, bbb=2, ddd=4}
Here is another example of HashMap in Java.
import java.util.Map;
import java.util.Set;
public class hashmapdemo
{
public static void main(String args[])
{
hashmapdemo obj = new hashmapdemo();
obj.g();
}
public void g()
{
Map <String, Integer > record = new HashMap< String, Integer >();
record.put("Anjana", 26);
record.put("Silpi", 27);
record.put("Heena", 28);
record.put("Adhiraj", 29);
System.out.println(record.get("Heena"));
Set<String> keys = record.keySet();
for(String i : keys)
{
System.out.println(i + ":" +record.get(i));
}
}
}
Output :
28
Adhiraj:29
Anjana:26
Silpi:27
Heena:28
Another example of hashmap :-
{
public static void main(String args[])
{
Map<Integer, String> map = new HashMap<Integer, String<();
map.put(10, "Chandan");
map.put(20, "Adhiraj");
map.put(30, "Anngraj");
System.out.println(map.get(10));
for(Map.Entry m : map.entrySet())
{
System.out.println(m.getKey()+ " " + m.getValue());
}
System.out.println("Printing the value using set");
Set<Integer> keys = map.keySet();
for(int i : keys)
{
System.out.println(i + " " + map.get(i));
}
}
}
Chandan
20 Adhiraj
10 Chandan
30 Anngraj
Printing the value using set
20 Adhiraj
10 Chandan
30 Anngraj
TreeMap in Java
TreeMap class implements Map interface similar to HashMap class.
The main difference between them is that HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys.
TreeMap is unsynchronized collection class which means it is not suitable for thread-safe operations until unless synchronized explicitly.
Here is the example of TreeMap in Java.
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class hashmapdemo
{
public static void main(String args[])
{
hashmapdemo obj = new hashmapdemo();
obj.g();
}
public void g()
{
//Map<String, Integer> record = new HashMap<String, Integer>();
TreeMap<Integer, String> record = new TreeMap<String, Integer>();
record.put(26, "Anjana");
record.put(28, "Heena");
record.put( 29, "Adhiraj");
record.put(27, "Silpi");
System.out.println(record.get(28));
for(Integer i : record.keySet())
{
System.out.println(i + ":" +record.get(i));
}
}
}
Output :
Heena
26:Anjana
27:Silpi
28:Heena
29:Adhiraj
Another example of Treemap :-
{
public static void main(String args[])
{
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(10, "Chandan");
map.put(20, "Adhiraj");
map.put(30, "Anngraj");
System.out.println(map.get(10));
for(Map.Entry m : map.entrySet())
{
System.out.println(m.getKey()+ " " + m.getValue());
}
System.out.println("Printing the value using set");
Set<Integer> keys = map.keySet();
for(int i : keys)
{
System.out.println(i + " " + map.get(i));
}
}
}
Chandan
10 Chandan
20 Adhiraj
30 Anngraj
Printing the value using set
10 Chandan
20 Adhiraj
30 Anngraj
HashMap vs TreeMap
HashMap vs HashTable vs SynchronizedHashMap vs ConcurrentHashMap
HashMap | LinkedHashMap | TreeMap |
---|---|---|
HashMap allows one null key. | LinkedHashMap permits one null key. | TreeMap does not permit null keys. |
HashMap permits multiple null values | LinkedHashMap allows multiple null values. | TreeMap allows null values. |
HashMap does not maintain any order. | LinkedHashMap maintains the insertion order. | TreeMap maintains keys in a sorted order. |
HashMap :- Not thread-safe, can have one null key & multiple null values.
Map<String, Integer>mp = new HashMap<String, Integer>();
// Map Interface is required to create object of HashMap class.
Hashtable :- Thread-safe, slow performance, null key and null values are not allowed.
SynchronizedMap :- Thread - safe, slow performance, one null key and multiple null values are allowed.
Collections.synchronizedMap(=new HashMap<String, Integer>());
ConcurrentHashMap :- Thread-safe, fast performance, null key and null values are not allowed.
ConcurrentHashMap<String, Integer> chm = new ConcurrentHashMap<String, Integer>();
//No Map interface is required to create the object of ConcurrentHashMap class.
//ConcurrenthashMap does not throw any ConcurrentModificationException.