In Java, you use TreeMap. You define the class that implements Comparable and write compareTo method to define the order. Then declare the Map as TreeMap. When adding data to a TreeMap, it automatically sort by key for you.