Set

[TOC]

特性

没有重复元素

Set接口

implementtation_of_the_set_interface.png
6个实现类,2个接口

HashSet

a_hash_table_with_chained_overflow.png
保持不重复的对象(可以是任意的,不用统一)
底层使用hashmap实现,存对象为键(保证不重复),值为标志位
遍历使用hashmap的 map.keySet().iterator(); 方法
常用于 消除重复元素(需要重写 hashcode()和equal()方法)
并发:不同步、线程不安全、迭代器快速失效

LinkedHashSet

a_linked_hash_table.png

底层使用LinkedHashMap实现
链表使用双向链表实现(有序)

用于有序消除重复元素(重写hashcode与equal)
并发:不同步、线程不安全、迭代器快速失效

CopyOnWriteArraySet

底层使用CopyOnWriteArrayList实现
不适用于大量查找和插入操作
适用于:白名单,黑名单,商品类目的访问和更新场景

并发:线程安全、读写分离迭代器

EnumSet

初始化
enumset为抽象类,不可直接new
底层使用RegularEnumSet(long-64),或者JumboEnumSet(long数组)如果枚举值个数小于等于64,则静态工厂方法中创建的就是RegularEnumSet,大于64的话就是JumboEnumSet。
静态构造函数

1
2
3
4
5
6
7
8
9
<E extends Enum<E>> EnumSet<E> of(E first, E... rest)
// create a set initially containing the specified elements
<E extends Enum<E>> EnumSet<E> range(E from, E to)
// create a set initially containing all of the elements in
// the range defined by the two specified endpoints
<E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
// create a set initially containing all elements in elementType
<E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
// create a set of elementType, initially empty

适用于求 交集、并集、补集

有序Set和巡航Set

SortSet接口

sortedset.png
实现类为TreeSet

获取第一个和最后一个元素
1
2
E first() // return the first element in the set
E last() // return the last element in the set
比较
1
Comparator<? super E> comparator()
获取范围视图
1
2
3
4
5
6
//获取大于等于fromElement,小于toElement
SortedSet<E> subSet(E fromElement, E toElement)
//获取小于toElement
SortedSet<E> headSet(E toElement)
//获取大于等于fromElement
SortedSet<E> tailSet(E fromElement)

navigableset.png

获取第一个和最后一个元素
1
2
3
4
E pollFirst() // retrieve and remove the first (lowest) element,
// or return null if this set is empty
E pollLast() // retrieve and remove the last (highest) element,
// or return null if this set is empty
获取范围视图
1
2
3
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,E toElement, boolean toInclusive)
NavigableSet<E> headSet(E toElement, boolean inclusive)
NavigableSet<E> tailSet(E fromElement, boolean inclusive)
获取最近比配的数据
1
2
3
4
5
6
7
8
E ceiling(E e) // return the least element in this set greater than
// or equal to e, or null if there is no such element(大于等于)
E floor(E e) // return the greatest element in this set less than
// or equal to e, or null if there is no such element(小于等于)
E higher(E e) // return the least element in this set strictly
// greater than e, or null if there is no such element(大于)
E lower(E e) // return the greatest element in this set strictly
// less than e, or null if there is no such element(小于)
导航反转有序数
1
2
3
NavigableSet<E> descendingSet() // return a reverse-order view of
// the elements in this set
Iterator<E> descendingIterator() // return a reverse-order iterator

TreeSet

底层算法使用 红黑树
使用TreeMap类实现
有序

并发:不同步、线程不安全、迭代器快速失效

ConcurrentSkipListSet

插入图解:
modifying_a_linked_list.png
查找图解
searching_a_skip_list.png

底层ConcurrentSkipListMap实现
随机因子(用于随机选择level1的个数
线程安全的)
确定层数:level设置默认为1,然后对rnd进行一个右位移操作后在与1进行与操作后来确定需要分为几层
cas保证线程安全

Set的比较实现

comparative.png

0%