Tuesday 3 February 2015

Java : Collection Framework : TreeMap (EntrySet)


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

TreeMapExample.java
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/*
 * Example of entrySet() method.
 */
public class TreeMapExample
{
    public static void main( String[] args )
    {

        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

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

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

        /*
         * A map entry (key-value pair).
         * 
         * Returns a Set view of the mappings contained in this map.
         */
        Set<Map.Entry<Integer, String>> entrySet = treeMap.entrySet();

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

        System.out.println("-----------------------");
        System.out.println("Key" +" | " + "value");
        System.out.println("-----------------------");
        
        for( Map.Entry<Integer, String> entry : entrySet )
        {
            System.out.println(entry.getKey() + "   | " +entry.getValue());
        }

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

entrySet : [1=Cat, 2=Dog, 3=Ball, 4=Apple]

-----------------------
Key | value
-----------------------
1   | Cat
2   | Dog
3   | Ball
4   | Apple
To Download TreeMapDemoEntrySet Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoEntrySet.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