TreeMap key
In this chapter you will learn:
- How to get first key in TreeMap
- How to get the last key in a TreeMap
- How to get key relative a give value
- 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;
/*from j av a 2s . c om*/
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 va 2 s . 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:
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;
//java 2 s . 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 ja v a2s .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:
Home » Java Tutorial » Map