TreeMap key

In this chapter you will learn:

  1. How to get first key in TreeMap
  2. How to get the last key in a TreeMap
  3. How to get key relative a give value
  4. How to get least key value relative to a key value

First key in TreeMap

The key value pairs in the TreeMap are ordered by the key values.

K firstKey() returns the first (lowest) key currently in this map.

import java.util.TreeMap;
//  j a  v a 2s. c  o  m
public class MainClass {
  public static void main(String args[]) {
    TreeMap map = new TreeMap();
    map.put("Virginia", "Richmond");
    map.put("Massachusetts", "Boston");
    map.put("New York", "Albany");
    map.put("Maryland", "Annapolis");

    if (!map.isEmpty()) {
      Object last = map.lastKey();
      boolean first = true;
      do {
        if (!first) {
          System.out.print(", ");
        }
        System.out.print(last);
        last = map.headMap(last).lastKey();
        first = false;
      } while (last != map.firstKey());
      System.out.println();
    }
  }
}

The output:

Last key in TreeMap

K lastKey() Returns the last (highest) key currently in this sorted map.

import java.util.TreeMap;
/* j a v  a  2s .  co  m*/
public class MainClass {
  public static void main(String args[]) {
    TreeMap map = new TreeMap();
    map.put("Virginia", "Richmond");
    map.put("Massachusetts", "Boston");
    map.put("New York", "Albany");
    map.put("Maryland", "Annapolis");

    if (!map.isEmpty()) {
      Object last = map.lastKey();
      boolean first = true;
      do {
        if (!first) {
          System.out.print(", ");
        }
        System.out.print(last);
        last = map.headMap(last).lastKey();
        first = false;
      } while (last != map.firstKey());
      System.out.println();
    }
  }
}

The output:

Lower key value

Map.Entry<K,V> lowerEntry(K key) Returns the greatest key less than the given key, or null if there is no such key.

import java.util.TreeMap;
//  j a va 2s  .  c  o  m
public class Main {

  public static void main(String[] args) {
    TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
    db.put(1000, new Product("D", 350));
    db.put(1011, new Product("p", 15.75));
    db.put(1102, new Product("M", 8.50));
    db.put(2023, new Product("A", 150));
    db.put(2034, new Product("T", 9.99));

    System.out.println(db.subMap(1000, 1999) + "\n");

    System.out.println(db.tailMap(1011) + "\n");

    System.out.println(db.headMap(2023));

    System.out.println("First key higher than 2034: " + db.higherKey(2034));
    System.out.println("First key lower than 2034: " + db.lowerKey(2034));
  }
}

class Product {
  String desc;

  double price;

  Product(String desc, double price) {
    this.desc = desc;
    this.price = price;
  }

  public String toString() {
    return "Description=" + desc + ", Price=" + price;
  }
}

Higher key

K higherKey(K key) Returns the least key strictly greater than the given key.

import java.util.TreeMap;
/*from j  av  a  2s .  co  m*/
public class Main {

  public static void main(String[] args) {
    TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
    db.put(1000, new Product("D", 350));
    db.put(1011, new Product("p", 15.75));
    db.put(1102, new Product("M", 8.50));
    db.put(2023, new Product("A", 150));
    db.put(2034, new Product("T", 9.99));

    System.out.println(db.subMap(1000, 1999) + "\n");

    System.out.println(db.tailMap(1011) + "\n");

    System.out.println(db.headMap(2023));

    System.out.println("First key higher than 2034: " + db.higherKey(2034));
    System.out.println("First key lower than 2034: " + db.lowerKey(2034));
  }
}

class Product {
  String desc;

  double price;

  Product(String desc, double price) {
    this.desc = desc;
    this.price = price;
  }

  public String toString() {
    return "Description=" + desc + ", Price=" + price;
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get a sub map whose key values are less than a value
Home » Java Tutorial » Collections

List interface
List add/insert elements
List clear/remove elements
List search
List element get and set
List and its Iterator
List size, empty
List conversion, to array
List to sublist
List comparison
ArrayList
ArrayList Creation
ArrayList add/insert
ArrayList get/set element
ArrayList clear/remove
ArrayList search
ArrayList copy and shallow copy
ArrayList size, trim to size and capacity
ArrayList to array
LinkedList class
LinkedList creation
LinkedList add/insert elements
LinkedList get elements
LinkedList search
LinkeList replace/set elements
LinkedList remove element
LinkedList copy
LinkedList iterator
LinkedList peek element
LinkedList pop/push element
LinkedList conversion
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap
HashSet
HashSet element adding
HashSet element removing and clearing
HashSet clone
HashSet iterator
HashSet properties
TreeSet
TreeSet elements adding
TreeSet subSet
NavigableSet
LinkedHashSet
Iterator
ListIterator
List filling
List reversing
List rotating and shuffling
List sorting
List element swap
List element replacing
List copy
List binary search
Collection unmodifiable
Collection synchronized
Collection singleton
Collection max/min value
Empty Collections
Comparator
Comparable
Enumeration
EnumSet
EnumMap Class
PriorityQueue