Java集合-Collection集合体系Set分支及Collections工具类

文章目录

    • 一、COllection集合体系-Set分支
      • 1. HashSet集合
      • 2. LinkedHashSet集合
      • 3. TreeSet集合
    • 二、Collections工具类
      • 1. COllections类概述

一、COllection集合体系-Set分支

1. HashSet集合

  • Set集合特点
    • 存取顺序不一致,元素唯一
  • HashSet
    • 底层数据结构是哈希表,是由数组和链表构成的
    • 线程不安全,集合元素可以是null
  • HashSet元素唯一性的保证
    • 存储过程
      • 当向HashSet集合中存储一个元素时,首先会调用hashcode方法计算该元素的hashcode值
      • 根据hashcode值确定元素在数组中的位置
      • 如果两个元素的hashcode值相等,接下来调用元素的equals方法,来进行比较,如果equals值不相等,则将该元素与原来的元素通过链表的形式链接起来存储。如果相等,则认为两个元素为同一元素,那么就不存储该元素,实现元素唯一
      • 基本类型的包装类型都已经重写了hashcode方法与equals方法,如果是自定义类的对象,需要自己重写两个方法
    • 唯一性的保证
      • 是靠重写hashcode方法与equals方法来确保
    • HashSet存储时的优化
      • 如果数组中某一位置的元素链表过长,就会影响查询速率,所以应该尽可能的让每一个元素的hashcode值不一样,以减少链过长的问题
  • 代码示例
public class Student {//学生类private String name;private int age;//私有成员变量public Student() {}//空参构造public Student(String name, int age) {//有参构造this.name = name;this.age = age;}@Overridepublic String toString() {//toString方法重写return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}//get方法public int getAge() {return age;}
}
public class MyTest {//编写程序,存储自定义对象到HashSet集合中,并遍历public static void main(String[] args) {HashSet<Student> stuHashSet = new HashSet<>();//创建HashSet集合对象stuHashSet.add(new Student("张三", 20));stuHashSet.add(new Student("李四", 22));stuHashSet.add(new Student("王五", 24));//给对象中存储元素for (Student student : stuHashSet) {//遍历输出System.out.println(student);}}
}
//运行结果,存取顺序不一致
//Student{name='王五', age=24}
//Student{name='李四', age=22}
//Student{name='张三', age=20}

2. LinkedHashSet集合

  • LinkedHashSet
    • 底层数据结构是链表和数据结构
    • 链表确保了元素的有序性,哈希表确保了元素的唯一性

3. TreeSet集合

  • TreeSet
    • 底层使用二叉树进行存储
    • 元素唯一,且大小有序(不是存取有序),可以将存入的数据按照某一定的顺序自动进行排序
    • 比如存入{4,5,3,1,2,6,7},将得到{1,2,3,4,5,6,7}
  • TreeSet 唯一有序的原理图

  • TreeSet的两种排序方法

    • 自然排序
      • 要求元素要实现Comparable接口,并重写该接口的compareTo方法,在方法的内部实现具体的比较逻辑,返回值为0表示两个元素相等
      • 代码示例
    public class Student implements Comparable {//学生类,实现了Comparable接口private String name;private int age;//私有成员变量public Student() {}//空参构造public Student(String name, int age) {//有参构造this.name = name;this.age = age;}@Overridepublic String toString() {//toString方法重写return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}//get方法public int getAge() {return age;}@Overridepublic int compareTo(Object o) {//自然排序,重写Comparable接口的compareTo方法Student stu = (Student) o;int num = this.getName().compareTo(stu.getName());int num1 = num == 0 ? this.getAge() - stu.getAge() : num;return num1;}
    }
    public class MyTest2 {// 对TreeSet集合采用自然排序方式排序public static void main(String[] args) {TreeSet<Student> stuTreeSet = new TreeSet<>();//创建TreeSet对象stuTreeSet.add(new Student("张三", 20));stuTreeSet.add(new Student("李四", 22));stuTreeSet.add(new Student("王五", 24));//给对象中存储元素for (Student student : stuTreeSet) {//遍历打印System.out.println(student);}}
    }
    //运行结果
    //Student{name='张三', age=20}
    //Student{name='李四', age=22}
    //Student{name='王五', age=24}
    
    • 比较器排序
      • 使用TreeSet类的带比较器Comparator的构造方法,可以使用匿名内部类的形式,在构造器中重写compare方法,自定义比较逻辑
      • 代码示例
    public class Student {//学生类private String name;private int age;//私有成员变量public Student() {}//空参构造public Student(String name, int age) {//有参构造this.name = name;this.age = age;}@Overridepublic String toString() {//toString方法重写return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}//get方法public int getAge() {return age;}
    }
    public class MyTest1 {//TreeSet集合,采用比较器排序方式public static void main(String[] args) {TreeSet<Student> stuTreeSet = new TreeSet<>(new Comparator<Student>() {//利用带比较器的有参构造创建对象@Overridepublic int compare(Student s1, Student s2) {//重写比较方法int num = s1.getName().compareTo(s2.getName());int num1 = num == 0 ? s1.getAge() - s2.getAge() : num;return num1;}});stuTreeSet.add(new Student("张三", 20));stuTreeSet.add(new Student("李四", 22));stuTreeSet.add(new Student("王五", 24));//给对象中存储元素for (Student student : stuTreeSet) {//遍历打印System.out.println(student);}}
    }
    //运行结果
    //Student{name='张三', age=20}
    //Student{name='李四', age=22}
    //Student{name='王五', age=24}
    
  • 代码示例

    • 编写一个程序,获取10个1至20的随机数,要求随机数不能重复
    public class Randoms {//产生10个1-100之间的随机数,要求随机数不能重复public static void main(String[] args) {Random random = new Random();//创建随机数对象TreeSet<Integer> randomTreeSet = new TreeSet<>(new Comparator<Integer>() {@Overridepublic int compare(Integer i1, Integer i2) {//比较器return i1 - i2;}});while (randomTreeSet.size() < 10) {//产生10个随机数randomTreeSet.add(random.nextInt(100) + 1);//将产生的随机数添加进集合里}System.out.println(randomTreeSet);//打印随机数}
    }
    

二、Collections工具类

1. COllections类概述

  • Collection集合体系的一个工具类,用来对Collection体系集合进行操作,类似于Arrays类对于数组
  • 成员方法
    • public static void sort(List list)
      • 将集合元素按照默认的自然顺序进行升序排序
    • public static int binarySearch(List<?> list,T key)
      • 在集合中按照二分查找法查找指定的元素
    • public static T max(Collection<?> coll)
      • 返回集合中元素的最大值
    • public static void reverse(List<?> list)
      • 将集合中元素反转
    • public static void shuffle(List<?> list)
      atic int binarySearch(List<?> list,T key)
      • 在集合中按照二分查找法查找指定的元素
    • public static T max(Collection<?> coll)
      • 返回集合中元素的最大值
    • public static void reverse(List<?> list)
      • 将集合中元素反转
    • public static void shuffle(List<?> list)
      • 将集合中的元素位置随机打乱