LinkedHashMap And TreeMap

LinkedHashMap extends  HashMap:-
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
import java.util.*;

public class LinkedHashMapDemo {
        public static void main(String args[]) {
                LinkedHashMap Lhm = new LinkedHashMap();
                Lhm.put(1, "Gyan");
                Lhm.put(6, "Ankit");
                Lhm.put(5, "Arun");
                Lhm.put(4, "Anand");
                Lhm.put(3, "Ram");
                System.out.println("The Entries of LinkedHashMap are : " +Lhm);
        }
}
Output:-

The Entries of LinkedHashMap are :
 {1=Gyan, 6=Ankit, 5=Arun, 4=Anand, 3=Ram}

TreeMap implements NavigableMap:-
This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key’s class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.
import java.util.TreeMap;
1.
2.public class TreeMapExample {
3.
4.  public static void main(String[] args) {
5.  
6.    //create TreeMap object
7.TreeMap treeMap = new TreeMap();
8.
9.//add key value pairs to TreeMap
10. treeMap.put("1","One");
11. treeMap.put("2","Two");
12.treeMap.put("3","Three");

13.boolean Exists=treeMap.containsKey("1");
14.    System.out.println("1 exists in TreeMap ? : " + Exists);
15.  }
16.}
17.
18./*
19.Output would be
20.1 exists in TreeMap ? : true
21.*/

People who read this post also read :



0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More