subMap(K fromKey, K toKey)

SortedMap<K,V> subMap(K fromKey, K toKey)
Returns the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
 
import java.util.TreeMap;

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;
  }
}
  
Home 
  Java Book 
    Collection  

TreeMap:
  1. TreeMap Class
  2. new TreeMap<K, V> ()
  3. entrySet()
  4. firstKey()
  5. get(K k)
  6. headMap(K toKey)
  7. headMap(K toKey, boolean inclusive)
  8. higherKey(K key)
  9. lastKey()
  10. lowerKey(K key)
  11. put(K k, V v)
  12. TreeMap size()
  13. subMap(K fromKey, K toKey)
  14. tailMap(T fromKey)
  15. TreeMap values()