treemap是java中基于红黑树实现的有序映射,能按键的自然顺序或自定义comparator自动排序,适用于需要键有序的场景,其插入、删除和查找操作的时间复杂度为o(log n);与hashmap(无序,基于哈希表,平均时间复杂度o(1))和linkedhashmap(保持插入顺序,基于哈希表加链表)不同,treemap的优势在于有序性,适合范围查询和按序遍历;在并发环境下,treemap本身非线程安全,可通过collections.synchronizedsortedmap进行包装或使用concurrentskiplistmap来实现线程安全,后者在高并发场景下性能更优。

Java集合框架中,
TreeMap
Comparator
TreeMap
TreeMap
AbstractMap
SortedMap
put
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
// 示例1: 使用键的自然顺序
public class TreeMapBasicUsage {
public static void main(String[] args) {
// 创建一个TreeMap,键(Integer)会按自然顺序(升序)排列
Map<Integer, String> studentScores = new TreeMap<>();
studentScores.put(95, "张三");
studentScores.put(88, "李四");
studentScores.put(72, "王五");
studentScores.put(95, "赵六"); // 键相同会覆盖值
System.out.println("按分数升序排列的学生:");
for (Map.Entry<Integer, String> entry : studentScores.entrySet()) {
System.out.println("分数: " + entry.getKey() + ", 姓名: " + entry.getValue());
}
// 示例2: 使用自定义Comparator进行降序排序
// 如果我们想让分数从高到低排列,就需要提供一个Comparator
Map<Integer, String> studentScoresDesc = new TreeMap<>(Comparator.reverseOrder());
studentScoresDesc.put(95, "张三");
studentScoresDesc.put(88, "李四");
studentScoresDesc.put(72, "王五");
System.out.println("\n按分数降序排列的学生:");
for (Map.Entry<Integer, String> entry : studentScoresDesc.entrySet()) {
System.out.println("分数: " + entry.getKey() + ", 姓名: " + entry.getValue());
}
// 获取特定键的值
System.out.println("\n分数95的学生是: " + studentScores.get(95)); // 输出:赵六
// 移除一个键值对
studentScores.remove(72);
System.out.println("\n移除分数72后的学生列表:");
for (Map.Entry<Integer, String> entry : studentScores.entrySet()) {
System.out.println("分数: " + entry.getKey() + ", 姓名: " + entry.getValue());
}
}
}可以看到,
TreeMap
立即学习“Java免费学习笔记(深入)”;
自定义排序:TreeMap如何使用Comparator接口?
很多时候,键的自然顺序(比如数字的升序、字符串的字典序)并不能满足我们的需求。比如,我们可能想让分数从高到低排列,或者根据一个自定义对象的某个特定属性来排序。这时候,
Comparator
Comparator
compare(T o1, T o2)
TreeMap
Comparator
TreeMap
Comparator
TreeMap
Comparable
在我看来,
Comparator
TreeMap
Comparable
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
class MyObject {
String name;
int value;
public MyObject(String name, int value) {
this.name = name;
this.value = value;
}
@Override
public String toString() {
return "{" + name + ", " + value + "}";
}
}
public class TreeMapCustomComparator {
public static void main(String[] args) {
// 自定义Comparator:根据MyObject的value属性降序排序
Comparator<MyObject> myObjectComparator = new Comparator<MyObject>() {
@Override
public int compare(MyObject o1, MyObject o2) {
// 注意:如果想降序,o2.value - o1.value
// 如果想升序,o1.value - o2.value
return Integer.compare(o2.value, o1.value);
}
};
Map<MyObject, String> customSortedMap = new TreeMap<>(myObjectComparator);
customSortedMap.put(new MyObject("A", 10), "数据A");
customSortedMap.put(new MyObject("B", 50), "数据B");
customSortedMap.put(new MyObject("C", 20), "数据C");
customSortedMap.put(new MyObject("D", 50), "数据D"); // 注意:如果Comparator认为键相等,会覆盖
System.out.println("按MyObject的value降序排列的Map:");
for (Map.Entry<MyObject, String> entry : customSortedMap.entrySet()) {
System.out.println("键: " + entry.getKey() + ", 值: " + entry.getValue());
}
// 另一个例子:按字符串长度排序(升序)
Map<String, Integer> stringLengthMap = new TreeMap<>(Comparator.comparingInt(String::length));
stringLengthMap.put("apple", 5);
stringLengthMap.put("banana", 6);
stringLengthMap.put("cat", 3);
stringLengthMap.put("dog", 3); // 注意:如果长度相同,会按字典序排,因为Comparator.comparingInt只比较了长度
System.out.println("\n按字符串长度升序排列的Map:");
for (Map.Entry<String, Integer> entry : stringLengthMap.entrySet()) {
System.out.println("键: " + entry.getKey() + ", 值: " + entry.getValue());
}
}
}在
Comparator
compare
TreeMap
TreeMap
TreeMap与HashMap、LinkedHashMap有什么区别?
在Java集合框架的Map家族里,
TreeMap
HashMap
LinkedHashMap
HashMap
put
get
remove
LinkedHashMap
HashMap
LinkedHashMap
TreeMap
TreeMap
TreeMap
put
get
remove
HashMap
选择哪个Map,很大程度上取决于你对数据顺序的需求。如果顺序不重要,
HashMap
LinkedHashMap
TreeMap
TreeMap
TreeMap在并发环境下的考量与线程安全性
谈到集合框架,尤其是在多线程环境中,线程安全性是一个绕不开的话题。对于
TreeMap
TreeMap
TreeMap
put
remove
这是因为
TreeMap
那么,在并发环境下,我们如何安全地使用类似
TreeMap
外部同步: 最简单直接的方式是使用
Collections.synchronizedSortedMap()
TreeMap
SortedMap
import java.util.Collections; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; // ... 在多线程中使用 SortedMap<Integer, String> syncTreeMap = Collections.synchronizedSortedMap(new TreeMap<>()); // 现在对syncTreeMap的所有操作都是线程安全的
这种方式虽然简单,但同步粒度较大,每次操作都需要获取锁,在高并发场景下可能会成为性能瓶颈。
使用并发集合: Java的
java.util.concurrent
ConcurrentSkipListMap
SortedMap
TreeMap
Collections.synchronizedSortedMap()
TreeMap
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentMap; // ... 在多线程中使用 ConcurrentMap<Integer, String> concurrentTreeMap = new ConcurrentSkipListMap<>(); // concurrentTreeMap现在是线程安全的,并且并发性能更优
ConcurrentSkipListMap
ConcurrentSkipListMap
TreeMap
总结来说,虽然
TreeMap
ConcurrentSkipListMap
以上就是Java集合框架如何使用TreeMap进行键值排序_Java集合框架有序映射的操作教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号