TreeMap : TreeMap « Collections « Java Tutorial






import java.util.TreeMap;

public class ProductDB {

  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;
  }
}








9.29.TreeMap
9.29.1.TreeMap Class
9.29.2.Viewing Sub Maps
9.29.3.Working with End Points
9.29.4.Get Synchronized Map from TreeMap
9.29.5.Check if a particular key exists in TreeMap
9.29.6.Check if a particular value exists in TreeMap
9.29.7.Get Head Map from TreeMap
9.29.8.Get lowest and highest key stored in TreeMap
9.29.9.Get Set view of Keys from TreeMap
9.29.10.Get TreeMap Size
9.29.11.Get Sub Map from TreeMap
9.29.12.Get Tail Map from TreeMap
9.29.13.Iterate through the values of TreeMap
9.29.14.Remove all values from TreeMap
9.29.15.Remove value from TreeMap
9.29.16.TreeMap
9.29.17.Cache Map