软件编程
位置:首页>> 软件编程>> java编程>> Java 深入浅出掌握Collection单列集合Set

Java 深入浅出掌握Collection单列集合Set

作者:乔巴菌儿  发布时间:2023-10-07 18:49:32 

标签:Java,单列集合,Collection

前言

Hello!上一期我大致讲解了关于Collection单列集合以及它的子接口List集合的概述、特点和遍历等,今天我为大家讲解关于Collection集合的另一个子接口-->Set集合的相关知识点。

Java 深入浅出掌握Collection单列集合Set

Set集合

Set概述

【1】Set集合类似于一个集装箱,程序可以依次将这些货物(多个对象)装运进此集装箱,而Set集合往往不能记住装运这些货物的顺序(元素的添加顺序),且不允许装运相同的货物。

【2】Set集合不允许包含相同的元素,如果试图把两个相同元素加入同一个Set集合中,则添加操作失败,add()方法返回false,且新元素不会被加入。

Set特点

  • 元素存取无序

  • 没有索引、只能通过迭代器或增强for循环遍历

  • 不能存储重复元素


public class SetDemo_01 {
   public static void main(String[] args) {
       Set<String> set = new HashSet<>();

//添加元素
       set.add("Hello");
       set.add("World");
       set.add("Java");
       set.add("Hello");//Set不包含重复元素的集合,(相同的,只输出一个)

//Set没有索引、只能通过迭代器或增强for循环遍历且元素存取无序
       //迭代器遍历
       Iterator<String> it = set.iterator();
       while(it.hasNext()) {
           String s = it.next();
           System.out.println(s);
       }

//增强for
       for (String s : set) {
           System.out.println(s);
       }
   }
}

HashSet集合

HashSet概述

HashSet是Set接口的典型实现,大多数时候使用Set集合时就是使用这个实现类。HashSet按Hash算法来存储集合中的元素,因此具有很好的存取和查找性能。底层数据结构是哈希表。

哈希值:

哈希值简介:是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值

如何获取哈希值:Object类中的public int hashCode():返回对象的哈希码值

哈希值的特点:(1)同一个对象多次调用hashCode()方法返回的哈希值是相同的;(2)默认情况下,不同对象的哈希值是不同的。而重写hashCode()方法,可以实现让不同对象的哈希值相同

HashSet特点

  • 底层数据结构是哈希表

  • 对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致

  • 没有带索引的方法,所以不能使用普通for循环遍历

  • 由于是Set集合,所以不包含重复元素的集合


public class HashSetDemo_01 {
   public static void main(String[] args) {
       //创建集合对象
       HashSet<String> hs = new HashSet<>();

//添加元素
       hs.add("Hello");
       hs.add("World");
       hs.add("Java");
       hs.add("Java");//由于是Set集合,所以是不包含重复元素的集合.相同的元素,只输出一个

//没有带索引的方法,所以不能使用普通for循环遍历
       //迭代器方式
       Iterator<String> it = hs.iterator();
       while (it.hasNext()) {
           String s = it.next();
           System.out.println(s);
       }
       System.out.println("----------------");

//增强for
       for (String s : hs) {
           System.out.println(s);
       }
   }
}

HashSet集合保证元素唯一性源码分析

HashSet集合保证元素唯一性的原理:

  • 根据对象的哈希值计算存储位置( 1.如果当前位置没有元素则直接存入; 如果当前位置有元素存在,则进入第二步)

  • 当前元素的元素和已经存在的元素比较哈希值(1. 如果哈希值不同,则将当前元素进行存储; 如果哈希值相同,则进入第三步)

  • 通过equals()方法比较两个元素的内容(1. 如果内容不相同,则将当前元素进行存储;2.如果内容相同,则不存储当前元素)

HashSet集合保证元素唯一性的图解

Java 深入浅出掌握Collection单列集合Set

LinkedHashSet集合

LinkedHashSet概述与特点

他是Set集合典型实现类HashSet的子类,继承了set集合的所有功能和特点,同时把其中的重要特点给修改,把无序变为有序。

  • 哈希表和链表实现的Set接口,具有可预测的迭代次序

  • 由链表保证元素有序,也就是说元素的存储和取出顺序是一致的

  • 由哈希表保证元素唯一,也就是说没有重复的元素


public class LinkedHashSetDemo {
   public static void main(String[] args) {
       //创建LinkedHashSet集合对象
       LinkedHashSet<String> lhs = new LinkedHashSet<>();

lhs.add("Hello");
       lhs.add("World");
       lhs.add("Java");

//由哈希表保证元素唯一,也就是说没有重复的元素
       lhs.add("Java");

/*哈希表和链表实现的Set接口,具有可预测的迭代次序
       由链表保证元素有序,也就是说元素的存储和取出顺序是一致的*/
       //遍历集合
       for (String s : lhs) {
           System.out.println(s);
       }
   }
}

TreeSet集合

TreeSet是SortedSet接口的实现类,TreeSet可以确保集合元素处于排序状态。

TreeSet特点

  • 元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法

  • 没有带索引的方法,所以不能使用普通for循环遍历

  • 由于是Set集合,所以不包含重复元素的集合

TreeSet(排序方式)

TreeSet():根据其元素的自然排序进行排序。TreeSet会调用集合元素的compareTo(Objec obj)方法来比较元素之间的大小关系,然后将集合元素按升序排列,这就是自然排序。

TreeSet(Comparator comparator) :根据指定的比较器进行排序。

这里我给出视频讲解里的关于比较器排序Comparator的使用的案例:

Java 深入浅出掌握Collection单列集合Set

  • 学生类


public class Student {
   private String name;
   private int age;

public Student() {
   }

public Student(String name, int age) {
       this.name = name;
       this.age = age;
   }

public String getName() {
       return name;
   }

public void setName(String name) {
       this.name = name;
   }

public int getAge() {
       return age;
   }

public void setAge(int age) {
       this.age = age;
   }
}
  • 测试类


public class TreeSetDemo {
   public static void main(String[] args) {
       //创建集合对象
       TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
           @Override
           public int compare(Student s1, Student s2) {
               //this.age - s.age
               //s1,s2
               int num = s1.getAge() - s2.getAge();
               int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
               return num2;
           }
       });

//创建学生对象
       Student s1 = new Student("xishi", 29);
       Student s2 = new Student("wangzhaojun", 28);
       Student s3 = new Student("diaochan", 30);
       Student s4 = new Student("yangyuhuan", 33);

Student s5 = new Student("linqingxia",33);
       Student s6 = new Student("linqingxia",33);

//把学生添加到集合
       ts.add(s1);
       ts.add(s2);
       ts.add(s3);
       ts.add(s4);
       ts.add(s5);
       ts.add(s6);

//遍历集合
       for (Student s : ts) {
           System.out.println(s.getName() + "," + s.getAge());
       }
   }
}

来源:https://blog.csdn.net/EVILDOERyyds/article/details/121165759

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com