Friday 6 June 2014

TreeMap : entrySet() : Get All Mappings in Set format

public Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.

import java.util.*;

class TreeMapEntrySet{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Set<Map.Entry<Integer,String>> entrySet;
  
  myMap = new TreeMap<> ();
  
  /* Add Data to myMap */
  myMap.put(1, "a");
  myMap.put(2, "ab");
  myMap.put(3, "abc");
  myMap.put(4, "abcd");
  
  entrySet = myMap.entrySet();
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  System.out.println("Elements in entrySet are");
  System.out.println(entrySet);
 }
}

Output
Elements in myMap are
{1=a, 2=ab, 3=abc, 4=abcd}
Elements in entrySet are
[1=a, 2=ab, 3=abc, 4=abcd]




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment