Sort based on keys is simple , just dump the map into TreeMap (TreeMap is sorted by nature) Map myMap = new HashMap(); // put keys and values... ..... Map sortedMap = new TreeMap(myMap); If you want to sort your HashMap based on values : HashMap map1 = new HashMap(); map1.put("cat",5); map1.put("cow",4); map1.put("dog",3); map1.put("horse",2); List mpKeys = new ArrayList(map1.keySet()); List mpValues = new ArrayList(map1.values()); HashMap map = new LinkedHashMap(); TreeSet sortedSet = new TreeSet(mapValues); Object[] sortedArray = sortedSet.toArray(); int size = sortedArray.length; // descending for (int i=size-1; i>=0; i--) System.out.println(mpKeys.get(mpValues.indexOf(sortedArray[i]))+" "+sortedArray[i]); Any discussions will be appreciated.
Comments