练习19——TreeSet类的使用

1、创建Employee1类

package test4;

import java.util.Objects;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/21
 * @since 1.0.0
 */
public  class Employee1{
    private String name;
    private Integer age;
    private MyDate1 birthday;

    public Employee1(String name, Integer age, MyDate1 birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Employee1)) return false;
        Employee1 employee1 = (Employee1) o;
        return getAge() == employee1.getAge() &&
                getName().equals(employee1.getName()) &&
                getBirthday().equals(employee1.getBirthday());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge(), getBirthday());
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public MyDate1 getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate1 birthday) {
        this.birthday = birthday;
    }
}

2、MyDate类

package test4;

import java.util.Objects;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/18
 * @since 1.0.0
 */
public  class MyDate1 implements Comparable {
    private int year;
    private int month;
    private int day;

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MyDate1)) return false;
        MyDate1 myDate1 = (MyDate1) o;
        return getYear() == myDate1.getYear() &&
                getMonth() == myDate1.getMonth() &&
                getDay() == myDate1.getDay();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getYear(), getMonth(), getDay());
    }

    public MyDate1() {
        super();
    }

    public MyDate1(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public String toDateString(){
        return year + "年" + month + "月" + day + "日";
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

3、实现TreeSet类

package test4;

import org.junit.Test;
import test2.Person;

import java.util.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/21
 * @since 1.0.0
 */
public class TestSet {
    @Test
    public void testTreeSet4(){

        TreeSet set = new TreeSet(new Comparator(){
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Employee1 && o2 instanceof Employee1){
                    Employee1 e1 = (Employee1)o1;
                    Employee1 e2 = (Employee1)o2;
                    int i = e1.getBirthday().compareTo(e2.getBirthday());
                    if(i == 0){
                        return -e1.getName().compareTo(e2.getName());
                    }
                    return i;
                }
                return 0;
            }
        });
        set.add(new Employee1("余淮",35,new MyDate1(1984,06,05)));
        set.add(new Employee1("阿芳",35,new MyDate1(1984,06,05)));
        set.add(new Employee1("耿耿",35,new MyDate1(1984,06,05)));
        set.add(new Employee1("余周周",36,new MyDate1(1983,01,26)));
        set.add(new Employee1("林扬",37,new MyDate1(1982,03,28)));
        set.add(new Employee1("温淼",34,new MyDate1(1985,07,04)));


        for(Object str : set){
            System.out.println(str);
        }

    }



    @Test
    //先按生日排序,再按name排序
    public void testTreeSet3(){
        Comparator com = new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Employee1 && o2 instanceof Employee1){
                    Employee1 e1 = (Employee1)o1;
                    Employee1 e2 = (Employee1)o2;

                    MyDate1 birth1 = e1.getBirthday();
                    MyDate1 birth2 = e2.getBirthday();
                    if (birth1.getYear() != birth2.getYear()){
                        return birth1.getYear() - birth2.getYear();
                    }else{
                        if(birth1.getMonth() != birth2.getMonth()){
                            return birth1.getMonth() - birth2.getMonth();
                        }else{
                            if(birth1.getDay() != birth2.getDay()){
                                return birth1.getDay() - birth2.getDay();
                            }else {
                                return e1.getName().compareTo(e2.getName());
                            }
                        }
                    }
                }
                return 0;
            }
        };

            TreeSet set = new TreeSet(com);
            //姓名为中文的时候并不是按照汉语拼音顺序排列的
                set.add(new Employee1("ww",35,new MyDate1(1984,06,05)));
                set.add(new Employee1("rr",35,new MyDate1(1984,06,05)));
                set.add(new Employee1("hh",35,new MyDate1(1984,06,05)));
                set.add(new Employee1("yy",36,new MyDate1(1983,01,26)));
                set.add(new Employee1("uu",37,new MyDate1(1982,03,28)));
                set.add(new Employee1("xx",34,new MyDate1(1985,07,04)));

        Iterator i = set.iterator();
        while(i.hasNext()){
            System.out.println(i.next());
        }
    }

    @Test
    public void testTreeSet2(){
        //TreeSet的定制排序
        Comparator com = new Comparator(){
            //向TreeSet中添加Person类的对象,在这个compare()方法中,
            // 指明是按照哪个属性排序的。
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Person && o2 instanceof Person){
                    Person p1 = (Person)o1;
                    Person p2 = (Person)o2;
                    int i = p1.getAge() - p2.getAge();
                    if(i == 0){
                        return p1.getName().compareTo(p2.getName());
                    }
                    return i;
                }
                return 0;
            }
        };
        //将此对象作为形参传递给TreeSet的构造器中
        TreeSet set = new TreeSet(com);
        //向TreeSet中添加Comparator接口中的compare方法中涉及的类的对象
        set.add(new Person("AA",18));
        set.add(new Person("CC",15));
        set.add(new Person("HH",16));
        set.add(new Person("DD",21));
        set.add(new Person("UU",90));
        set.add(new Person("EE",90));

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

    @Test
    public void testLinkedHashSet(){
        //遍历时候较快,添加较慢
        Set set = new LinkedHashSet();
        set.add(123);
        set.add(456);
        set.add(456);
        set.add('a');
        set.add("bc");
        set.add(null);

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    @Test
    public void testTreeSet1(){
        //自然排序
        Set set = new TreeSet();
        set.add(new Person("AA",18));
        set.add(new Person("CC",15));
        set.add(new Person("HH",16));
        set.add(new Person("DD",21));
        set.add(new Person("UU",90));
        set.add(new Person("EE",90));

//        set.add("aa");

        for(Object str : set){
            System.out.println(str);
        }

    }

    @Test
    public void testTreeSet(){
        //自然排序
        //向treeset中添加的元素必须是同一类的
        Set set = new TreeSet();
//        set.add(123);
//        set.add(456);
        set.add('f');
        set.add('w');
        set.add('a');
        set.add('k');
        set.add('q');

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    @Test
    public void testHashSet(){
        //插入较快,对比linkedHashSet
        Set set = new HashSet();
        set.add(123);
        set.add(456);
        set.add('a');
        set.add("bc");
        set.add(null);

        Person p1 = new Person("jack",18);
        Person p2 = new Person("jack",18);
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());

        set.add(p1);
        set.add(p2);
        System.out.println(p1.equals(p2));

        System.out.println(set.size());
        System.out.println(set);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值