Java program to SORT MAP based on KEYS

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]); 
} 



} 
}


Vinay

I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube