You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
privatetransientHashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map//作为map的value,没什么用处privatestaticfinalObjectPRESENT = newObject();
HashSet( )
/** * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has * default initial capacity (16) and load factor (0.75). */publicHashSet() {
map = newHashMap<>();
}
size( )
/** * Returns the number of elements in this set (its cardinality). * * @return the number of elements in this set (its cardinality) */publicintsize() {
returnmap.size();
}
isEmpty( )
/** * Returns <tt>true</tt> if this set contains no elements. * * @return <tt>true</tt> if this set contains no elements */publicbooleanisEmpty() {
returnmap.isEmpty();
}
/** * The backing map. */privatetransientNavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing MapprivatestaticfinalObjectPRESENT = newObject();
构造方法
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
publicTreeSet() {
this(newTreeMap<E,Object>());
}
//传入一个进行元素排序的比较器publicTreeSet(Comparator<? superE> comparator) {
this(newTreeMap<>(comparator));
}
//传入一个集合,将其中的元素添加至TreeSet中publicTreeSet(Collection<? extendsE> c) {
this();
addAll(c);
}
//创建TreeSet,并将s中的全部元素都添加到TreeSet中publicTreeSet(SortedSet<E> s) {
this(s.comparator());
addAll(s);
}
前言
上一篇重点看了下 HashMap 以及简单说了说 LinkedHashMap,今天看下 HashSet 和 TreeSet。
HashSet
HashSet 很简单,没什么内容。先看下两个属性和几个主要的方法源码:
两个属性
HashSet( )
size( )
isEmpty( )
contains(Object)
add(E)
remove(Object)
OK,这几个方法看完了就没了,一眼就可以看出来 HashSet 的元素就像 HashMap 的 key 一样。换句话说,HashMap 是 实现HashSet 的支撑。这个没啥意思,再来看下相比之下更有意思的 TreeSet
TreeSet
其实大多数类或者接口,看名字就知道有什么主要特性了,Set, 它是一个元素不重复的集合,TreeSet,它就是一个可以基于二叉树对元素进行排序的不可重复集合。TreeSet 是基于 TreeMap 实现的。TreeSet 中的元素支持2种排序方式:自然排序 或者 根据创建 TreeSet 时提供的 Comparator 进行排序。这取决于使用的构造方法。
成员变量
构造方法
排序方式
让元素自身具备比较性,需要元素实现Comparable接口,覆盖compareTo方法,
这种方式也称为元素的自然排序,或者叫做默认排序。
当元素自身不具备比较性时,或者具备的比较性不是所需要的时候,
需要让集合自身具备比较性。在集合初始化时,就有了比较性
需要定义一个实现了Comparator接口的比较器,覆盖compare方法,并将该类对象作为
参数传递给TreeSet集合的构造函数
add(E)
这篇内容较少,因为它都是基于两个相关的Map实现的,也比较简单
The text was updated successfully, but these errors were encountered: