-HASH MAP
-TREE MAP
-lINKED HASH MAP
HASH MAP
-It contains values based on key
-Contains unique elements (Duplication not allowed)
-Maintain no order
-One null key only possible, many null value can be posssible
TREE MAP
-It contains values based on key
-Contains unique elements
-Maintain ascending order ( descending order can possible)
-no null key, many null value can be posssible
LINKED HASH MAP
-It contains values based on key
-Contains unique elements
-Maintain order (TREE MAP)
-one null key only possible, many null value can be posssible (HASH MAP)
HASH MAP CODE
imports java.util.*; - Library file
HashMap hm = new Hashmap(); - Class File
Hashmap class has two main methods
-put() - put the element to Hashmap
-get() - get the element from Hashmap
Note: Retrive the data form Hashmap using Iteration but "Set" Collection only support the Iteration which does not support the Iteration to Hashmap. Iteration only possible Set. We can not using the Iteration to Hashmap, List,..etc.
MAP - print the elements with Iterator code..
import java.util.*;
public class MAP {
public static void main(String[] args) {
//Creating a HashMap | LinkedHashMap | TreeMap
Map hm = new HashMap();
//Map hm = new LinkedHashMap();
//Map hm = new TreeMap();
//put the elements to the map
hm.put(100,"Najathi,Naharni,Nazaaha,Thahlan");
hm.put(101,"Najathi");
hm.put(103,"Nazaaha");
hm.put(102,"Naharni");
hm.put(104,"Thahlan");
hm.put(104,"Thahani");
//Get a set of the entries
Set set = hm.entrySet();
//Get an Iterator
Iterator iterator = set.iterator();
//Display elements
while(iterator.hasNext()){
Map.Entry me = (Map.Entry) iterator.next();
System.out.print(me.getKey()+" ");
System.out.println(me.getValue());
}
}
}
MAP - print the elements without Iterator code..
import java.util.HashMap;
import java.util.Map;
public class MapUsingFor {
public static void main(String[] args) {
//Creating a HashMap | LinkedHashMap | TreeMap
Map map = new HashMap();
//put the elements to the map
map.put(100,"Najathi,Naharni,Nazaaha,Thahlan");
map.put(101,"Najathi");
map.put(103,"Nazaaha");
map.put(102,"Naharni");
map.put(104,"Thahlan");
map.put(104,"Thahani");
for(Integer key:map.keySet()){
System.out.println("Key = "+key);
}
for(String value:map.values()){
System.out.println("Value = "+value);
}
}
}
No comments:
Post a Comment