当我打开 TreeSet 类的源代码时,有以下代码:
public boolean contains(Object o) {
return m.containsKey(o);
}
m 是一个 NavigableMap,它是一个接口。那么实施在哪里呢?它绝对不在 TreeSet 本身。
当我打开 TreeSet 类的源代码时,有以下代码:
public boolean contains(Object o) {
return m.containsKey(o);
}
m 是一个 NavigableMap,它是一个接口。那么实施在哪里呢?它绝对不在 TreeSet 本身。
From the source for TreeSet
:
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
public TreeSet() {
this(new TreeMap<E,Object>());
}
So m
should be a TreeMap
(or possibly another implementation of NavigableMap
if another class in the same package calls that constructor).