Java tutorial
import java.util.Comparator; import java.util.SortedMap; import java.util.TreeMap; public class Main { public static void main(String[] args) { Comparator<String> keyComparator = Comparator.comparing(String::length) .thenComparing(String::compareToIgnoreCase); SortedMap<String, String> sMap = new TreeMap<>(keyComparator); sMap.put("CSS", "style"); sMap.put("HTML", "mark up"); sMap.put("Oracle", "database"); sMap.put("XML", "data"); SortedMap<String, String> subMap = sMap.subMap("CSS", "XML"); System.out.println(subMap); // Get the first and last keys String firstKey = sMap.firstKey(); String lastKey = sMap.lastKey(); System.out.println("First Key: " + firstKey); System.out.println("Last key: " + lastKey); } }