Java中TreeMap的内部工作原理

2023年 8月 28日 54.2k 0

TreeMap is a class of Java Collection Framework that implements NavigableMap Interface. It stores the elements of the map in a tree structure and provides an efficient alternative to store the key-value pairs in sorted order. Internally, TreeMap uses a red-black tree, which is a self-balancing binary search tree. A TreeMap must implement the Comparable Interface or a custom Comparator so that it can maintain the sorting order of its elements otherwise, we will encounter a java.lang.ClassCastException. This article aims to explain how TreeMap works internally in Java.

Java中TreeMap的内部工作原理

要理解TreeMap的内部工作原理,有必要对红黑树算法有一个概述,因为TreeMap使用它来存储元素。

Relation of Red Black Tree and TreeMap

红黑树是一种自平衡的二叉搜索树,其属性如下所述:

  • 每个节点都包含一个额外的位,由红色或黑色表示。这些颜色用于确保树保持平衡。

  • The color of root node is always black.

  • 一个红色节点不能有与其邻居相同颜色的另一个节点

  • 从根节点到空节点,所有路径上的黑色节点数量必须相同

Java中TreeMap的内部工作原理

Now, let's see the structure of TreeMap:

Java中TreeMap的内部工作原理

If we add an element to the TreeMap, it will add the first entry in the first place. From the second element, it will check whether the key of current entry is greater or smaller than the previous entry. The keys with lower value will be added to the left and the ones with greater value will be added to the right of Parent entry.

TreeMap的构造函数

To use the TreeMap collection in our program, we first need to create an instance of the TreeMap class. For that Java provides the following constructors:

  • TreeMap():它将创建一个空的TreeMap集合

  • TreeMap(Map mapInstance): 我们可以使用另一个映射中的条目创建一个树映射

  • TreeMap(Comparator comparatorInstance):它允许我们创建一个空的树映射,通过使用比较器进行排序。

  • TreeMap(SortedMap sortedmapInstance): We can also create a tree map with the entries from a sorted map.

Let's discuss a few examples to have a better understanding of the above discussed points.

Example

在以下示例中,我们将创建一个空的TreeMap,然后使用'put()'方法将一些元素存储到其中。我们将使用for-each循环打印其详细信息。结果将按照排序顺序显示。

import java.util.*;
public class Example1 {
public static void main(String[] args) {
// creating a TreeMap
TreeMap TrMap = new TreeMap();
// Adding elements in the map
TrMap.put("Kurti", 4000);
TrMap.put("Shirt", 3000);
TrMap.put("TShirt", 1500);
TrMap.put("Watch", 2000);
TrMap.put("Perfume", 2500);
// printing the details of map
System.out.println("Elements of the map: ");
// iterating through the map
for (String unKey : TrMap.keySet()) {
// printing details of each node
System.out.println("Item: " + unKey + ", Price: " +
TrMap.get(unKey));
}
String frstKey = TrMap.firstKey(); // accessing first key
String lstKey = TrMap.lastKey(); // accessing last key
System.out.println("Accessing name of first key in Map: " +
frstKey);
System.out.println("Accessing name of last key in Map: " +
lstKey);
}
}

登录后复制

Output

Elements of the map:
Item: Kurti, Price: 4000
Item: Perfume, Price: 2500
Item: Shirt, Price: 3000
Item: TShirt, Price: 1500
Item: Watch, Price: 2000
Accessing name of first key in Map: Kurti
Accessing name of last key in Map: Watch

登录后复制

结论

我们开始这篇文章时定义了TreeMap,并在下一节中讨论了它的内部工作原理。TreeMap使用红黑树来存储其元素,这是一种能够自我平衡的二叉搜索树。此外,我们还讨论了一个示例来说明TreeMap的工作原理。

以上就是Java中TreeMap的内部工作原理的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论