Java-TreeSet集合


1.直接上代码,

package com.shujia.wyh.day21;
import java.util.TreeSet;

/*
        使用TreeSet存储自定义学生对象,并且使用比较器排序,使学生按照年龄长短进行排序
 */
public class TreeSetDemo1 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet set = new TreeSet<>(new Student2());

        //创建学生对象
        Student2 s1 = new Student2("xiaohu", 18);
        Student2 s2 = new Student2("xiaoming", 17);
        Student2 s3 = new Student2("zitai", 18);
        Student2 s4 = new Student2("letme", 19);
        Student2 s5 = new Student2("xiaohu", 20);
        Student2 s6 = new Student2("xiaohu", 18);

        //将学生对象添加到集合中
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

        //遍历集合
        for (Student2 student2 : set) {
            System.out.println(student2);
        }
    }
}

代码二:

package com.shujia.wyh.day21;
import java.util.Comparator;
import java.util.TreeSet;
/*
        使用TreeSet存储自定义学生对象,并且使用比较器排序,使学生按照年龄长短进行排序(匿名内部类的方式实现)
 */
public class TreeSetDemo2 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet set = new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student2 o1, Student2 o2) {
                //主要条件:姓名的长短
                int i = o1.getName().length() - o2.getName().length();
                //隐含条件
                //姓名长度一样,内容不一定一样
                int i2 = (i == 0) ? o1.getName().compareTo(o2.getName()) : i;
                //姓名一样,年龄还不一定一样
                int i3 = (i2 == 0) ? o1.getAge() - o2.getAge() : i2;

                return i3;
            }
        });

        //创建学生对象
        Student2 s1 = new Student2("xiaohu", 18);
        Student2 s2 = new Student2("xiaoming", 17);
        Student2 s3 = new Student2("zitai", 18);
        Student2 s4 = new Student2("letme", 19);
        Student2 s5 = new Student2("xiaohu", 20);
        Student2 s6 = new Student2("xiaohu", 18);

        //将学生对象添加到集合中
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

        //遍历集合
        for (Student2 student2 : set) {
            System.out.println(student2);
        }
    }
}

学生类对象

package com.shujia.wyh.day21;

import java.util.Comparator;

public class Student2 implements Comparator {
    private String name;
    private int age;

    public Student2() {
    }

    public Student2(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 "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compare(Student2 o1, Student2 o2) {
        //主要条件:姓名的长短
        int i = o1.name.length() - o2.name.length();
        //隐含条件
        //姓名长度一样,内容不一定一样
        int i2 = (i == 0) ? o1.name.compareTo(o2.name) : i;
        //姓名一样,年龄还不一定一样
        int i3 = (i2 == 0) ? o1.age - o2.age : i2;

        return i3;
    }
}