[英]What is the purpose of compare(key, key) in Java TreeMap put() method when root == null?
I am reading Java 1.8 TreeMap source code.我正在阅读 Java 1.8 TreeMap 源代码。 The put method is written as below:
put方法的写法如下:
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
...... //more code
}
When root == null, which means that it is the first time for us to put an Entry, why we still need to compare the key itself before we assign the entry to root?当root == null时,说明我们是第一次放Entry,为什么我们在把entry赋值给root之前还需要自己比较key呢?
BTW, compare method is:顺便说一句,比较方法是:
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
The comment explains why:评论解释了原因:
// type (and possibly null) check
This compare
will ensure that key
is not null
, and has the expected type, before it is stored in the map.此
compare
将确保key
不是null
,并且在存储到 map 之前具有预期的类型。
In the compare
method, as shown, the key
is either cast to a Comparable
, and has compareTo
invoked, or it is passed as an argument to the comparator
.如图所示,在
compare
方法中, key
要么被转换为Comparable
并调用compareTo
,要么作为参数传递给comparator
。 These operations will fail with either NullPointerException
s or ClassCastExceptions
if key
is null
, or not of the required type for the comparator
to handle.如果
key
是null
或不是comparator
处理所需的类型,这些操作将失败并出现NullPointerException
或ClassCastExceptions
。
The comment hints at what is going on.该评论暗示了正在发生的事情。
// type (and possibly null) check
A correctly implemented Comparator
or Comparable
will check that its arguments have types that it understands before comparing them.正确实现的
Comparator
或Comparable
将在比较它们之前检查其 arguments 是否具有它理解的类型。 It may also test if the arguments are not null
... depending on whether it implements an ordering where null
is or isn't allowed.它还可以测试 arguments 是否不是
null
... 取决于它是否实现了允许或不允许null
的排序。
By using compare
to compare the initial key to itself, the code is checking that key has an appropriate type, and optionally that it is not null
.通过使用
compare
将初始密钥与自身进行比较,代码正在检查密钥是否具有适当的类型,并且可选地检查它不是null
。
You commented:你评论说:
but the compare method seems not do the type and null check
但比较方法似乎不做类型和 null 检查
This is the code you are talking about.这是您正在谈论的代码。
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
The checks we are talking about happen as follows.我们正在谈论的检查发生如下。
When comparator
is null
:当
comparator
是null
时:
null
check on k1
occurs when k1.compareTo(k2)
is called.k1.compareTo(k2)
时,对k1
进行隐式null
检查。(Comparable)
cast to ensure that k1
is a Comparable
.(Comparable)
转换中进行类型检查以确保k1
是Comparable
。compareTo
method.compareTo
方法中。 When comparator
is not null
, the type check and the (optional in this case) null
check happen within the Comparator<K>.compare
method that we are calling.当
comparator
不是null
时,类型检查和(在本例中为可选) null
检查发生在我们调用的Comparator<K>.compare
方法中。
(The casts to (K)
are unchecked type casts and won't do any actual type checking.) (对
(K)
的转换是未经检查的类型转换,不会进行任何实际类型检查。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.