Friday, 30 January 2015

Java : Collection Framework : TreeMap (Constructor Accepts Map)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=CjJJUHfZ1go&list=UUhwKlOVR041tngjerWxVccw

TreeMapExample.java
import java.util.HashMap;
import java.util.TreeMap;

/*
 * Example of TreeMap(Map<? extends K,? extends V> m) Constructor.
 */
public class TreeMapExample
{
    public static void main( String[] args )
    {

        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

        hashMap.put(1, "Cat");
        hashMap.put(2, "Dog");
        hashMap.put(4, "Apple");
        hashMap.put(3, "Ball");

        System.out.println("hashMap : " + hashMap + "\n");

        /*
         * Constructs a new tree map containing the same mappings as the given
         * map, ordered according to the natural ordering of its keys. All keys
         * inserted into the new map must implement the Comparable interface.
         */

        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(hashMap);
        
        System.out.println("treeMap : " + treeMap + "\n");

    }
}
Output
hashMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}
To Download TreeMapDemoConsAcceptsMap Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoConsAcceptsMap.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge