Set 模拟 数学上 的概念

  • Set 覆盖了equals 和 hashcode方法
  • 如果两个Set对象包含相同的元素,那么他们就是相等的。

 

  • HashSet:散列表 乱序 性能最好 
  • TreeSet:红黑树 按元素值排序 比HashSet稍慢
  • LinkedHashSet:链式散列表 按插入顺序排序 代价稍高

 

  Collection<Type> noDups = new HashSet<Type>(c); // Collection c 去掉重复元素后变为 Collection noDups

  Collection<Type> noDups = new LinkedHashSet<Type>(c); // 保持Collection c中原始顺序 同时去掉重复元素

 

基本操作:size isEmpty add remove iterator

批量操作:containsAll addAll retainAll removeAll

数组操作 toArray toArray(new String[0]);

相关文章:

  • 2022-12-23
  • 2021-05-26
  • 2021-09-02
  • 2021-08-28
  • 2021-12-24
  • 2022-12-23
  • 2021-06-27
  • 2021-11-14
猜你喜欢
  • 2021-08-05
  • 2022-02-07
  • 2022-12-23
相关资源
相似解决方案