Java教程-Java TreeMap类

Java TreeMap类
Java TreeMap类是基于红黑树实现的。它提供了一种在排序顺序中高效存储键值对的方式。
关于Java TreeMap类的重要点如下:
- Java TreeMap根据键来存储值。它实现了NavigableMap接口并扩展了AbstractMap类。
- Java TreeMap只包含唯一的元素。
- Java TreeMap不能有空键,但可以有多个空值。
- Java TreeMap是非同步的。
- Java TreeMap维护升序排序。
TreeMap类声明
让我们看一下java.util.TreeMap类的声明。
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
TreeMap类参数
让我们看一下java.util.TreeMap类的参数。
- K:该映射维护的键的类型。
- V:该映射维护的映射值的类型。
Java TreeMap类的构造方法
构造方法 | 描述 |
---|---|
TreeMap() | 构造一个使用其键的自然顺序进行排序的空树映射。 |
TreeMap(Comparator<? super K> comparator) | 构造一个使用给定比较器comp进行排序的空基于树的映射。 |
TreeMap(Map<? extends K,? extends V> m) | 使用键的自然顺序对来自m的条目进行排序,初始化一个treemap。 |
TreeMap(SortedMap<K,? extends V> m) | 使用与sm相同的顺序初始化treemap,从SortedMap sm中获取条目。 |
Java TreeMap示例
import java.util.*;
class TreeMapExample {
public static void main(String args[]) {
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
输出:
100 Amit
101 Vijay
102 Ravi
103 Rahul
Java TreeMap示例:remove()
import java.util.*;
public class TreeMapExample {
public static void main(String args[]) {
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
System.out.println("Before invoking remove() method");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
map.remove(102);
System.out.println("After invoking remove() method");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
输出:
Before invoking remove() method
100 Amit
101 Vijay
102 Ravi
103 Rahul
After invoking remove() method
100 Amit
101 Vijay
103 Rahul
Java TreeMap示例:NavigableMap
import java.util.*;
class TreeMapExample {
public static void main(String args[]) {
NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
// 维护降序
System.out.println("descendingMap: " + map.descendingMap());
// 返回键小于或等于指定键的键值对
System.out.println("headMap: " + map.headMap(102, true));
// 返回键大于或等于指定键的键值对
System.out.println("tailMap: " + map.tailMap(102, true));
// 返回存在于指定键之间的键值对
System.out.println("subMap: " + map.subMap(100, false, 102, true));
}
}
输出:
descendingMap: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
headMap: {100=Amit, 101=Vijay, 102=Ravi}
tailMap: {102=Ravi, 103=Rahul}
subMap: {101=Vijay, 102=Ravi}
Java TreeMap示例:SortedMap
import java.util.*;
class TreeMapExample {
public static void main(String args[]) {
SortedMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
// 返回小于指定键的键值对
System.out.println("headMap: " + map.headMap(102));
// 返回大于或等于指定键的键值对
System.out.println("tailMap: " + map.tailMap(102));
// 返回存在于指定键之间的键值对
System.out.println("subMap: " + map.subMap(100, 102));
}
}
输出:
headMap: {100=Amit, 101=Vijay}
tailMap: {102=Ravi, 103=Rahul}
subMap: {100=Amit, 101=Vijay}
HashMap和TreeMap的区别
HashMap | TreeMap |
---|---|
1) HashMap可以包含一个空键。 | TreeMap不能包含任何空键。 |
2) HashMap不保持任何顺序。 | TreeMap保持升序排序。 |
Java TreeMap示例:Book
import java.util.*;
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class TreeMapExample {
public static void main(String[] args) {
Map<Integer, Book> map = new TreeMap<Integer, Book>();
Book b1 = new Book(101, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(102, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b3 = new Book(103, "Operating System", "Galvin", "Wiley", 6);
map.put(2, b2);
map.put(1, b1);
map.put(3, b3);
for (Map.Entry<Integer, Book> entry : map.entrySet()) {
int key = entry.getKey();
Book b = entry.getValue();
System.out.println(key + " Details:");
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
}
}
输出:
1 Details:
101 Let us C Yashwant Kanetkar BPB 8
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
3 Details:
103 Operating System Galvin Wiley 6