Requirment- To sort MAP based on key using Java
following is the source code-
package techartifact; import java.util.*; class SortMaps { public static void main(String args[]) { Map m= new LinkedHashMap(); m.put( 1 , 10 ); m.put( 3 , 11 ); m.put( 5 , 15 ); m.put( 2 , 13 ); m.put( 4 , 14 ); Collection c=m.keySet(); System.out.println(c); Object a[]; a = c.toArray(); Arrays.sort(a); for ( int i= 0 ;i<a.length;i++) { System.out.println((Object)a[i]); } } } |