Sunday 8 June 2014

TreeMap : values() : Get Collection view of values

public Collection<V> values()
Returns a Collection view of the values contained in this map.

import java.util.*;

class TreeMapValues{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Collection<String> values;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap */
  myMap.put(2, "w");
  myMap.put(4, "wx");
  myMap.put(6, "wxy");
  myMap.put(8, "wxyz");
  myMap.put(1, "m");
  myMap.put(3, "mn");
  myMap.put(5, "mno");
  myMap.put(7, "mnop");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  values = myMap.values();
  
  System.out.println("values in myMap are");
  System.out.println(values);
 }
}

Output
Elements in myMap are
{1=m, 2=w, 3=mn, 4=wx, 5=mno, 6=wxy, 7=mnop, 8=wxyz}
values in myMap are
[m, w, mn, wx, mno, wxy, mnop, wxyz]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment