Java Collection Tutorial - Java TreeMap(Comparator <? super K > comparator) Constructor


TreeMap(Comparator <? super K > comparator) constructor from TreeMap has the following syntax.

public TreeMap(Comparator <? super K> comparator)


In the following code shows how to use TreeMap.TreeMap(Comparator <? super K > comparator) constructor.

import java.util.Collections;
import java.util.TreeMap;
//from www  .java 2s  .c  om
public class Main {

  public static void main(String[] args) {
    TreeMap<Integer, String> db = new TreeMap<Integer, String>(Collections.reverseOrder());
    db.put(1000, "1000");
    db.put(1011, "1011");
    db.put(1102, "1102");
    db.put(2023, "2023");
    db.put(2034, "2034");


The code above generates the following result.