集合类 Set

1 Set集合
(1)元素不重复
(2)没有带索引的方法,所以不能使用普通for循环遍历

2 HashSet
(1)元素不重复
(2)没有带索引的方法,所以不能使用普通for循环遍历
(3)底层结构是哈希表
(4)不保障存储和取出的元素顺序一致
(5)在使用HashSet时,内容相同的不同对象,HashSet认为是不重复的。如果我们要实现相同内容不重复,需要在相应的类中使用Alt+Insert自动生成的方式重写equals()和hashCode()方法

3 LinkedHashSet集合
哈希表和链表实现的Set接口,具有可预测的迭代次序,且元素不重复

4 TreeSet集合
(1)元素有序,不是指存储和取出的顺序,而是按照一定的顺序进行排序,具体排序方式取决于构造方法
无参TreeSet():根据元素的自然排序进行排序
TreeSet(Comparator omparator):根据指定的比较器进行排序
(2)没有带索引的方法,所以不能使用普通的for循环遍历
(3)不重复
(4)自然排序Comparable的使用
使用TreeSet集合存储自定义对象时,TreeSet是如何进行对象间的比较呢?
解决方法是:让元素所属的类实现Comparable接口,重写compareTo(Object o)方法,重写方法时,要注意写完整比较条件
示例:

public class Girl implements Comparable<Girl> {
    private String name;

    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;
    }

    private int age;

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

    @Override
    public int compareTo(Girl g) {
        //按年龄升序排列,年龄相同时按照姓名排序
        int num = this.age - g.age;
        return num == 0 ? this.name.compareTo(g.name) : num;
    }
}
import java.util.TreeSet;
public class TreeSetDemo {
    public static void main(String[] args) {
        Girl girl1 = new Girl("西施", 25);
        Girl girl2 = new Girl("王昭君", 20);
        Girl girl3 = new Girl("貂蝉", 20);
        Girl girl4 = new Girl("杨玉环", 28);

        TreeSet<Girl> treeSet = new TreeSet<Girl>();
        treeSet.add(girl1);
        treeSet.add(girl2);
        treeSet.add(girl3);
        treeSet.add(girl4);

        for (Girl girl : treeSet) {
            System.out.println(girl.getName() + ":" + girl.getAge());
        }
    }
}

(5)比较器Comparator的使用
用TreeSet集合存储自定义对象,带参构造方法使用的是比较器Comparator对元素进行排序。比较器排序就是让集合构造方法接收Comparator的实现类对象,重写compare(T o1,T o2)方法,重写方法时,要注意写完整比较条件
示例:

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

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

    public String getName() {
        return name;
    }

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

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo1 {
    public static void main(String[] args) {
        //匿名内部类
        TreeSet<Student> treeSet = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = 0;
                //按身高升序排序,身高相同时按年龄排序,升高年龄同时相同时按姓名排序
                num = s1.getHeight() - s2.getHeight();
                if(num == 0){
                    num = s1.getAge()- s2.getAge();
                }
                return num == 0 ? s1.getName().compareTo(s2.getName()) : num;
            }
        });

        Student stu1 = new Student("萧峰",188,33);
        Student stu2 = new Student("韦小宝",166,15);
        Student stu3 = new Student("张无忌",175,24);
        Student stu4 = new Student("杨过",178,21);
        Student stu5 = new Student("郭靖",180,25);

        treeSet.add(stu1);
        treeSet.add(stu2);
        treeSet.add(stu3);
        treeSet.add(stu4);
        treeSet.add(stu5);

        for (Student stu:treeSet){
            System.out.println(stu.getName()+":"+stu.getAge()+":"+stu.getHeight());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海阔天空2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值