Follow by Email

Sunday, September 26, 2010

Sorting the keys in a Map.....

Use a TreeMap and put your objects in it , the key may be anything like a String ,numaric etc.

TreeMap objectTreeMap=new TreeMap() ;

Now if you want to get the list of values from this map sorted on the bases of the key set then use following code



//**Get the keys in ascending order
Iterator iterator= objectTreeMap.navigableKeySet().iterator();

And then iterate to get the values

//Iterate the map to get values ordered by the Key of this map
while(iterator.hasNext()){
Object obj=iterator.next();
System.out.println("<<<<<<<<<<<<<<<<"+obj.toString());
}

No comments:

Post a Comment