• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Java | TreeMap
  1. Notes
  2. Example

  1. Notes
    Sorted entries
    Duplicates elements not allowed
  2. Example
    TreeMap<Integer, String> tm = new TreeMap<>();
    
    tm.clear();
    // tm.put(null, "x"); // java.lang.NullPointerException
    // tm.put(0, null); // OK
    tm.put(0, "a");
    tm.put(4444, "c");
    tm.put(55555, "b");
    tm.put(333, "d");
    tm.put(11, "f");
    tm.put(222, "e");
    tm.put(666666, "g");
    
    for (Map.Entry<Integer, String> entry : tm.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
    Output:
    0 = a
    11 = f
    222 = e
    3333 = d
    44444 = c
    555555 = b
    6666666 = g
© 2025  mtitek