Sort an HashMap based on the keys
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] a) {
Map<String, String> yourMap = new HashMap<String, String>();
yourMap.put("1", "one");
yourMap.put("2", "two");
yourMap.put("3", "three");
Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);
System.out.println(sortedMap);
}
}
//{1=one, 2=two, 3=three}
Related examples in the same category