Java集合之Set接口

Set接口的主要功能就是去重,它可以保证集合中不会有重复的元素(equals不相等)。

Set接口

Set接口

  • add (E e) 向列表内添加指定元素
  • addAll(Collection< ? extends E> c) addAll(int index, Collection< ? extends E> c) 向集合内(指定位置 index)添加另一集合的全部元素
  • get(int index)获取指定位置的元素
  • clear() 清空集合,可用于多次使用单个集合对象,节省资源
  • contains(Object o) 判断是否还有某个对象
  • containsAll(Collection< ?> c) 判断是否含有集合c的所有元素
  • parallelStream() java8新特性,使用fork/join框架自动并行处理
  • retainAll(Collection< ?> c)移除除c中元素的所有的其他元素
  • remove(Object o) 移除集合中的一个元素
  • removeAll(Collection< ?> c) 移除集合含有的集合c中的所有元素
  • removeIf(Predicate< ? super E> filter) 底层迭代调用Predicate的test方法。Predicate函数式接口主要用提供test()方法,该方法返回一个布尔变量
  • spliterator() 并行迭代器
  • stream() 返回集合的流资源,用于函数式运算
  • toArray() 转换成数组
  • toArray(T[] a) 将集合转换成对应对象类型的数组

HashSet类

HashSet的底层基于HashMap实现,元素存储在HashMap的值中,内部数据存储是无序的,同时HashSet不是线程安全的。
可以使用Collections工具类创建线程安全的HashSet:
Set s = Collections.synchronizedSet(new HashSet(...));

LinkedHashSet继承了HashSet,可以对照HashMap和LinkedHashMap,LinkedHashSet同样可以保证插入元素的顺序,其他的特性和HashSet一样。

SortedSet接口默认根据自然顺序对集合进行排序,保证内部集合有序。

SortedSet接口

集合内的元素根据自然顺序排序,或者通过一个对应的排序器排序(创建集合时指定)
comparator() 返回有序集合使用的排序器
first() 返回有序集合的第一个元素
headSet(E toElement) 返回toElement之前的所有元素
last() 返回有序集合的最后一个元素
spliterator()
subSet(E fromElement, E toElement) 返回从fromElement到toElement的元素,包含fromElement不包含toElement
tailSet(E fromElement) 返回fromElement以后的所有元素

NavigableSet接口继承了SortedSet,提供了更多的基于排序的方法。

NavigableSet接口

不允许插入null元素
ceiling(E e) 返回排序最靠前的集合中与e可以equal或者比e大的元素,没有的话返回null
descendingIterator() 降序返回集合的迭代器
descendingSet() 返回一个集合的降序
floor(E e) 返回集合中最靠后与e可以equal或者比e小的元素,没有的话返回null
headSet(E toElement) 返回toElement之前的所有元素,包含toElement
headSet(E toElement, boolean inclusive) 返回toElement之前的所有元素,isclusive决定是否包含toElement
higher(E e) 返回排序最靠前的集合中比e大的元素,没有的话返回null
iterator()
lower(E e) 返回排序最靠后的集合中比e小的元素,没有的话返回null
poolFirst() 获取并移除头部元素
poolLast() 获取并移除尾部元素
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
返回从 fromElement到toElement的集合,fromInclusive决定是否包含fromElement,toInclusive决定是否包含toElement
subSet(E fromElement, E toElement)
返回从 fromElement到toElement的集合,包含fromElement,不包含toElement
tailSet(E fromElement) 返回fromElement之后的所有元素,包含fromElement
tailSet(E fromElement, boolean inclusive) 返回fromElement之后的所有元素,inclusive决定是否包含fromElement

TreeSet类

TreeSet类

A NavigableSet implementation based on a TreeMap. The elements are ordered using their naturNavigableSet al ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

NavigableSet 的实现,底层数据基于TreeMap。底层数据的排序基于自然顺序或者新建对象时定义的比较器。

有序集合,底层实现为TreeMap
并发时不安全
ceiling(E e) 返回排序最靠前的集合中与e可以equal或者比e大的元素,没有的话返回null
descendingIterator() 降序返回集合的迭代器
descendingSet() 返回一个集合的降序
floor(E e) 返回集合中最靠后与e可以equal或者比e小的元素,没有的话返回null
headSet(E toElement) 返回toElement之前的所有元素,包含toElement
headSet(E toElement, boolean inclusive) 返回toElement之前的所有元素,isclusive决定是否包含toElement
higher(E e) 返回排序最靠前的集合中比e大的元素,没有的话返回null
iterator()
lower(E e) 返回排序最靠后的集合中比e小的元素,没有的话返回null
poolFirst() 获取并移除头部元素
poolLast() 获取并移除尾部元素
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
返回从 fromElement到toElement的集合,fromInclusive决定是否包含fromElement,toInclusive决定是否包含toElement
subSet(E fromElement, E toElement)
返回从 fromElement到toElement的集合,包含fromElement,不包含toElement
tailSet(E fromElement) 返回fromElement之后的所有元素,包含fromElement
tailSet(E fromElement, boolean inclusive) 返回fromElement之后的所有元素,inclusive决定是否包含fromElement

posted on 2018-11-21 17:48  ShoolyShooly  阅读(153)  评论(0编辑  收藏  举报

导航