Java集合之Set

HashSet

  特点
  元素存储和取出的顺序无序,且元素唯一。HashSet底层数据结构是哈希表, 哈希表是一个元素为链表的数组,综合了数组和链表的优点HashSet不是线程安全的。集合元素可以是null。
  保证元素唯一性
  当向 HashSet 集合中存入一个元素时,HashSet 会调用该对象的 hashCode() 方法来得到该对象的 hashCode 值,然后根据 hashCode 值决定该对象在 HashSet 中的存储位置。HashSet 集合判断两个元素相等的标准:两个对象通过 hashCode() 方法比较相等,并且两个对象的 equals() 方法返回值也相等。
  结论:HashSet 保证元素唯一性是靠元素重写hashCode()和equals()方法来保证的,如果不重写则无法保证。
  下面演示重写两个方法来存储自定义对象

import java.util.Objects;

public class Student {
    private String name;
    private int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {return name;}
    public int getAge() {return age;}
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
import java.util.HashSet;

public class Demo1 {
    public static void main(String[] args) {
        HashSet<Student> students = new HashSet<>();
        Student student = new Student("小明",3);
        Student student2 = new Student("小明",3);
        students.add(student);
        students.add(student2);
        for (Student student1 : students) {
            System.out.println(student1.getName()+" "+student.getAge());
        }
    }
}

  HashSet中一些常用的方法 ,很好理解,就不演示了。。

public boolen add(E e);  
如果此set中尚未包含指定元素,则添加指定元素
public void clear();移除此set中所有元素
public boolen contains();
如果此set包含指定元素,则返回true
public boolen isEmpty();
如果此set不包含任何元素,则返回true
public boolen remove(Object o);
如果指定元素存在此set中,则将其移除
public int size(); 返回此set中元素的数量

LinkedHashSet

  特点
  数据结构是链表和哈希表,链表保证有序,哈希表保证元素唯一,所以特点就是,元素有序,并且唯一
  LinkedHashSet中一些常用的方法 ,大多数是从HashSet继承的方法

public boolen add(E e);  
如果此set中尚未包含指定元素,则添加指定元素
public void clear();移除此set中所有元素
public boolen contains();
如果此set包含指定元素,则返回true
public boolen isEmpty();
如果此set不包含任何元素,则返回true
public boolen remove(Object o);
如果指定元素存在此set中,则将其移除
public int size(); 返回此set中元素的数量

TreeSet

   TreeSet集合的特点
  元素唯一,并且可以对元素进行排序
  排序分为自然排序和比较器排序,到底使用的是哪一种的排序取决于构造方法。
  注意:使用TreeSet集合进行元素的自然排序,那么对元素有要求,要求这个元素, 必须实现Comparable接口 否则无法进行自然排序。
   保证元素的唯一性是compareTo方法的返回值来确定,如果返回0,表示两个元素相等,则不重复存储。
   TreeSet保证元素唯一和自然排序的原理
   二叉树的数据结构 先存入一个树根 分两个叉, 存储元素时 跟树根比较 小的放在左边 大的放在右边,如果相等就不存储,取的时候按照 左中右的顺序来取。
   图示在这里插入图片描述

   演示自然排序和比较器排序

比较器排序:(Student类参考上面HashSet里的代码演示,但不需要重写那两个方法)
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        Student stu1 = new Student("小黑", 18);
        Student stu2 = new Student("小黑", 18);
        Student stu3 = new Student("小白", 20);
        TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge()-s2.getAge();
                int num1 = num==0?s1.getName().compareTo(s2.getName()):num;
                return num1;
            }
        });
        students.add(stu1);
        students.add(stu2);
        students.add(stu3);
        for (Student student : students) {
            System.out.println(student.getName()+" "+student.getAge());
        }
    }
}
输出:
小黑 18
小白 20
自然排序
public class Student implements Comparable<Student>{
    private String name;
    private int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {return name;}
    public int getAge() {return age;}
    public int compareTo(Student s){
        int num = this.getAge()-s.getAge();
        int num1 = num==0?this.getName().compareTo(s.getName()):num;
        return num;
    }
}

  HashSet中一些常用的方法 ,很好理解,就不演示了。。

public boolen add(E e);  
如果此set中尚未包含指定元素,则添加指定元素
public boolen addAll(E e); 
将指定collection中的所有元素添加到此set中
public Comparator<? super E>comparator();
返回对此set中的元素进行排序的比较器
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值