集合都实现了序列化接口
1,Set集合的唯一性
Set类集合
set可保证元素不重复,TreeSet依靠TreeMap进行实例化,TreeMap的put方法中利用了比较器排序,根据底层的树形结构,键值大的放右边,小的放左边。键值相等时,设置为新的键值,所以键相同,值覆盖。
还有从源码中可以看到当插入null键时会抛出空指针异常。
//TreeMap构造方法源码
public TreeSet() {
this(new TreeMap<E,Object>());
}
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
//TreeMap的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;
}
int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;