Thursday, 22 January 2015

Java : Collection Framework : HashMap (EntrySet)


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

HashMapExample.java
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

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

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

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

        /*
         * A map entry (key-value pair).
         * 
         * Returns a Set view of the mappings contained in this map.
         */
        Set<Map.Entry<Integer, String>> entrySet = hashMap.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
hashMap : {1=Apple, 2=Ball, 3=Cat}

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

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