java中的set集合

116 阅读3分钟

java中的set集合

1.HashSet集合

1.1HashSet的特点

  1. HashSet底层实现使用的是HashMap
  2. 不能保证元素的顺序,元素是无序的,不能有重复元素
  3. 集合元素允许是null
  4. HashSet线程不安全

1.2HashSet常用方法

  1. add(Object o):向Set集合中添加元素,不允许添加重复数据。
  2. size():返回Set集合中的元素个数
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         System.out.println("set = " + set);
         System.out.println(set.size());
     }
 }

注意:不会按照保存的顺序存储数据(顺序不定),遍历时不能保证下次结果和上次相同。且向HashSet集合中添加元素,HashSet add方法实质是map全局变量调用了put方法,将数据存到了key,因为HashMap的 key不允许重复,所以HashSet添加的元素也不允许重复。

  1. remove(Object o): 删除Set集合中的obj对象,删除成功返回true,否则返回false。
  2. isEmpty():如果Set不包含元素,则返回 true。
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         System.out.println(set.isEmpty());
         System.out.println(set.remove(Integer.valueOf(123)));
         System.out.println(set);
     }
 }
  1. clear(): 移除set中的所有元素
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         System.out.println(set);
         set.clear();
         System.out.println(set);
     }
 }
  1. iterator():返回在此Set中的元素上进行迭代的迭代器。
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         Iterator<Integer> iterator = set.iterator();
         while (iterator.hasNext()){
             System.out.println(iterator.next());
         }
     }
 }
  1. contains(Object o):判断集合中是否包含obj元素。
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         System.out.println(set.contains(123));
     }
 }
  1. 加强for循环遍历Set集合:
 public class Test1 {
     public static void main(String[] args) {
         HashSet<Integer> set = new HashSet<>();
         set.add(123);
         set.add(123);
         set.add(234);
         for (Integer integer : set) {
             System.out.println(integer);
         }
     }
 }

注意:因此set中的元素是没有顺序的,所有不能用下标来获取元素

2.LinkedHashSet集合

2.1LinkedHashSet集合的特点

  1. LinkedHashSet底层使用的是HashSet,同时使用链表维护元素的插入顺序
  2. 元素有序且唯一,链表保证了元素有序
  3. 哈希表保证了元素唯一
  4. 线程不安全

3.TreeSet集合

3.1TreeSet集合的特点

  1. TreeSet其内部使用的是TreeMap,TreeMap是基于红黑树实现的
  2. 插入数据内部有两种排序方法:自然排序(默认)、定制排序
  3. 无序:TreeSet会对插入的数据排序,所以输入的顺序和输出的顺序不一致
  4. 值不能为null
  5. 值唯一
  6. 线程不安全

3.2TreeSet的基本使用

  1. 插入是按字典序排序的
 public class Test1 {
     public static void main(String[] args) {
         TreeSet<Integer> ts = new TreeSet<>();
         ts.add(234);
         ts.add(123);
         ts.add(99);
         System.out.println(ts);
     }
 }
  1. 如果插入的是自定义对象 需要让类实现 Comparable 接口并且必须要重写compareTo
 import java.util.TreeSet;
 ​
 public class Person implements Comparable {
     String name;
     int age;
 ​
     Person(String name, int age) {
         this.name = name;
         this.age = age;
     }
 ​
 ​
     @Override
     public int compareTo(Object o) {
         Person p = (Person) o;
         if (this.name.compareTo(p.name) != 0) {
             return this.name.compareTo(p.name);
         } else {
             if (this.age > p.age) return 1;
             else if (this.age < p.age) return -1;
         }
         return 0;
     }
 }
 ​
 class Test {
     public static void main(String[] args) {
         TreeSet<Person> ts = new TreeSet<>();
         ts.add(new Person("abc", 18));
         ts.add(new Person("adf", 28));
         ts.add(new Person("abac", 20));
         ts.add(new Person("adf", 15));
         for (Person t : ts) {
             System.out.println(t.name+" "+t.age);
         }
     }
 }

输出

 abac 20
 abc 18
 adf 15
 adf 28

4.HashSet、LinkedHashSet、TreeSet的使用场景

HashSet:HashSet的性能基本上比LinkedHashSet和TreeSet要好,特别是添加和查询,这也是用的最多的两个操作

LinkedHashSet:LinkedHashSet的查询稍微慢一些,但是他可以维持元素的基本顺序。所以只有要求当插入顺序和取出顺序一致的时候,才会使用LinkedHashSet

TreeSet:只有需要对元素进行排序的时候使用

5.list和set集合的区别

5.1有序性

  • List保证了按插入顺序排序
  • Set存储和取出的顺序不一致

5.2唯一性

  • List可以重复
  • Set元素唯一

5.3获取元素

  • List可以通过索引直接操作元素
  • Set不能根据索引获取元素

总结

Set集合不能重复,常规使用HashSet,在需要添加顺序和取出顺序一致的时候使用LinkedHashSet,当需要排序时使用TreeSet