集合二--Set

目录:
Set
HashSet
LinkedHashSet
TreeSet
1、Set
**特点 :**无序且唯一
set集合不能存储重复的元素
Set集合的元素是按照某种排序规则存储的
2.HashSet
HashSet特点:
保证元素唯一性,保证元素唯一性是靠元素重写hashCode()和equals()方法来保证的,如果不重写则无法保证。

请编写程序,存储自定义对象到HashSet集合中,并遍历
public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + 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);
    }
}
public class HashSetDemo {
    public static void main(String[] args) {
        HashSet<Student> hashSet = new HashSet<>();
        hashSet.add(new Student("张三",23));
        hashSet.add(new Student("张三",23));
        hashSet.add(new Student("张三",24));
        hashSet.add(new Student("李四",23));
        hashSet.add(new Student("王五",26));
        Iterator<Student> iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //方式2
       for (Student stu:hashSet){
           System.out.println(stu);
       }
    }
}

3.LinkedHashSet
特点:
数据结构 有两个 链表和哈希表
链表保证有序 哈希表保证元素唯一
LinkedHashSet的概述: 元素有序 , 并且唯一
4.TreeSet
Treeset 的底层数据结构是二叉树, 唯一性是继承set的,靠重写hashcode和equals来保证唯一,二叉树实现了排序
答案:TreeSet集合中保证元素唯一性与排序有俩种方式
1>实现自然排序接口Comparable,重写ComparTO(T t)方法
2>实现比较器排序接口Compartor,重写ComparTO(T t1,T t2)方法

a)采用自然排序方式
b)采用比较器排序方式
public class Student implements  Comparable<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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int i = this.name.length() - o.name.length();
        //名字一样不能说明是同一个对象
        int i1 = i==0?this.name.compareTo(o.name):i;
        //名字一样了还不能说明是同一个对象
        int i3 = i1==0?this.age-o.age:i1;
        return i3;
    }
}
public class treeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student("张三",32));
        treeSet.add(new Student("张三111",34));
        treeSet.add(new Student("张三2222",37));
        treeSet.add(new Student("张三33333",39));
        treeSet.add(new Student("张三",33));
        treeSet.add(new Student("李四",32));
        treeSet.add(new Student("张三",32));
        for (Student  stu:treeSet){
            System.out.println(stu);
        }
        System.out.println("-----------------------------");
        TreeSet<Student> treeSet1 = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o1.getAge() - o2.getAge();//比较器排序
                int i1=i==0?o1.getName().compareTo(o2.getName()):i;
                return i1;
            }
        });
        treeSet1.add(new Student("张三",32));
        treeSet1.add(new Student("张三111",34));
        treeSet1.add(new Student("张三2222",37));
        treeSet1.add(new Student("张三33333",39));
        treeSet1.add(new Student("张三",33));
        treeSet1.add(new Student("李四",32));
        treeSet1.add(new Student("张三",32));
        for (Student stu:treeSet){
            System.out.println(stu);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值