HashMap Sort by Keys

 /*  
 HashSort.java: This program will demonstrate how to sort Hash by its Keys........  
 author:itsafiz@gmail.com   
 */  
 import java.util.*;   
 class HashSortbyKey  
 {  
      public static void main(String []args)  
           {  
                HashMap words = new HashMap();   
                words.put("3","three");                 
                words.put("1","one");  
                words.put("0","zero");  
                words.put("2","two");  
                Object [] keys = words.keySet().toArray();   
                Arrays.sort(keys);   
                for(int i=0;i<keys.length;i++)  
                     System.out.println(keys[i]+" = "+words.get(keys[i]));   
           }  
 }  

Post a Comment

0 Comments