java - set、 hashset、linkedset、treeset

主要:如果保证元素唯一

HashSet: set集合是无索引,不可以重复,存取的顺序不一致

  add( )   存储成功返回True/失败返回false

  hashset.clear():从此 set 中移除所有元素。

  hashset.remove(Object o):如果指定元素存在于此 set 中,则将其移除。

  hashset.isEmpty():如果此 set 不包含任何元素,则返回 true。

  hashset.contains(Object o):如果此 set 包含指定元素,则返回 true。

  hashset.size():返回此 set 中的元素的数量(set 的容量)。

注意:把对象装进hashset时,如果要重写equals方法,也得重写hashcode 方法,因为equals()相等的两对象hashcode 也是相同的。

 

 

LinkedHashSet:保证怎么存就怎么取

  

treeset:是用来对元素进行去重排序

 

posted @ 2018-11-19 15:31  红豆·  阅读(350)  评论(0编辑  收藏  举报