jdk 1.9之前与collection无差别1.9之后和list一样扩充了很多方法,新增of()方法;
元素不重复
主要子类:HashSet,TreeSet
无法使用get()方法获取指定索引数据的,
Module java.base
Package java.util
Interface Set
Type Parameters:
E - the type of elements maintained by this set
All Superinterfaces:
Collection
All Known Subinterfaces:
EventSet, NavigableSet
All Known Implementing Classes:
AbstractSet, ConcurrentHashMap.KeySetView, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, ReadOnlySetProperty, ReadOnlySetPropertyBase, ReadOnlySetWrapper, SetBinding, SetExpression, SetProperty, SetPropertyBase, SimpleSetProperty, TreeSet
public interface Set
extends Collection
HashSet:实现接口Set,不重复且无
序的,利用HashCode和equals共同判断是否是相同的元素,在java中元素重复判断均是由HashCode和equals完成,除tree(treeSet)等排序外
Module java.base
Package java.util
Class HashSet
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
java.util.HashSet
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Serializable, Cloneable, Iterable
Direct Known Subclasses:
JobStateReasons, LinkedHashSet
public class HashSet
extends AbstractSet
implements Set
TreeSet:实现Tree接口,无重复且升序排列,当保存的元素是一个类时必须继承接口comparable接口并覆写compareTo方法且比较所有属性大小,否则将会使得部分属性相同的数据是为一样而删除,利用comparable接口完成重复判断。
Module java.base
Package java.util
Class TreeSet
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
java.util.TreeSet
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Serializable, Cloneable, Iterable
public class TreeSet
extends AbstractSet
implements NavigableSet
评论区