Set的相关练习

一.自然排序和定制排序的练习

(1)定义一个 Employee 类。该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;并为每一个属性定义getter, setter 方法;并重写toString 方法输出 name, age, birthday。

(2)MyDate类包含private成员变量 year,month,day ;并为每一个属性定义 getter, setter方法;

(3)创建该类的5 个对象,并把这些对象放入 TreeSet 集合中分别按以下两种方式对集合中的元素进行排序,并遍历输出

问题一:使 Employee 实现 Comparable 接口,并按 name 排序

1.定义Employee类并实现Comparable接口

1)未使用泛型的写法

public class Employee implements Comparable{
    private String name;
    private int age;
    private MyDate birthday;

    public Employee() {
    }

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

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

    public MyDate getBirthday() {
        return birthday;
    }

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

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

    @Override
    //按name排序
    public int compareTo(Object o) {
        if(o instanceof Employee){
            Employee e=(Employee)o;
            return this.name.compareTo(e.name);
        }
        throw new RuntimeException("输入的类型不匹配");
    }
}

2)使用泛型后的写法

public class Employee implements Comparable<Employee>{
    private String name;
    private int age;
    private MyDate birthday;

    public Employee() {
    }

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

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

    public MyDate getBirthday() {
        return birthday;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
//指明泛型后的写法
    @Override
    public int compareTo(Employee o) {
        return this.name.compareTo(o.name);
    }
}

2.定义MyDate类

public class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate() {
    }

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = 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;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

3.进行测试

1)未使用泛型的写法

     @org.junit.Test
     //使Employee实现 Comparable接口,并按name排序
    public void test1(){
         TreeSet set=new TreeSet();
         Employee e1 = new Employee("wangxianzhi",41,new MyDate(334,5,4));
         Employee e2 = new Employee("simaqian",43,new MyDate(-145,7,12));
         Employee e3 = new Employee("yanzhenqin",44,new MyDate(709,5,9));
         Employee e4 = new Employee("zhangqian",51,new MyDate(-179,8,12));
         Employee e5 = new Employee("quyuan",21,new MyDate(-340,12,4));
         set.add(e1);
         set.add(e2);
         set.add(e3);
         set.add(e4);
         set.add(e5);

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

2)使用泛型后的写法 

    public void test1(){
         //TreeSet<Employee> set=new TreeSet<Employee>();
         TreeSet<Employee> set=new TreeSet<>();
         Employee e1 = new Employee("wangxianzhi",41,new MyDate(334,5,4));
         Employee e2 = new Employee("simaqian",43,new MyDate(-145,7,12));
         Employee e3 = new Employee("yanzhenqin",44,new MyDate(709,5,9));
         Employee e4 = new Employee("zhangqian",51,new MyDate(-179,8,12));
         Employee e5 = new Employee("quyuan",21,new MyDate(-340,12,4));
         set.add(e1);
         set.add(e2);
         set.add(e3);
         set.add(e4);
         set.add(e5);

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

问题二:创建TreeSet 时传入 Comparator 对象,按生日日期的先后排序。

1)未使用泛型的写法

@org.junit.Test
//创建 TreeSet时传入Comparator对象,按生日日期的先后排序。
//按照年月日的顺序来排  如果年相等比较月 月相等就比较日
    public void test2(){
    TreeSet set=new TreeSet(new Comparator() {
        @Override
        public int compare(Object o1, Object o2) {
            if(o1 instanceof Employee && o2 instanceof Employee){
                Employee e1=(Employee)o1;
                Employee e2=(Employee)o2;
                int compare1=Integer.compare(e1.getBirthday().getYear(),e2.getBirthday().getYear());
                if(compare1!=0){
                    return compare1;
                }
                int compare2=Integer.compare(e1.getBirthday().getMonth(),e2.getBirthday().getMonth());
                if(compare2!=0){
                    return compare2;
                }
                int compare3=Integer.compare(e1.getBirthday().getDay(),e2.getBirthday().getDay());
                return compare3;
            }
            throw new RuntimeException("输入的类型不匹配");
        }

    });


    Employee e1 = new Employee("wangxianzhi",41,new MyDate(1975,5,4));
    Employee e2 = new Employee("simaqian",43,new MyDate(1988,7,12));
    Employee e3 = new Employee("yanzhenqin",44,new MyDate(1970,5,9));
    Employee e4 = new Employee("zhangqian",51,new MyDate(2007,8,12));
    Employee e5 = new Employee("quyuan",21,new MyDate(2022,12,4));
    set.add(e1);
    set.add(e2);
    set.add(e3);
    set.add(e4);
    set.add(e5);

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

2)使用泛型后的写法 

   public void test2(){
    //指明泛型后的写法
    TreeSet<Employee> set=new TreeSet<>(new Comparator<Employee>() {
        @Override
        public int compare(Employee o1, Employee o2) {
            int compare1=Integer.compare(o1.getBirthday().getYear(),o2.getBirthday().getYear());
                if(compare1!=0){
                    return compare1;
                }
                int compare2=Integer.compare(o1.getBirthday().getMonth(),o2.getBirthday().getMonth());
                if(compare2!=0){
                   return compare2;
               }
               int compare3=Integer.compare(o1.getBirthday().getDay(),o2.getBirthday().getDay());
               return compare3;
        }
    });
    Employee e1 = new Employee("wangxianzhi",41,new MyDate(1975,5,4));
    Employee e2 = new Employee("simaqian",43,new MyDate(1988,7,12));
    Employee e3 = new Employee("yanzhenqin",44,new MyDate(1970,5,9));
    Employee e4 = new Employee("zhangqian",51,new MyDate(2007,8,12));
    Employee e5 = new Employee("quyuan",21,new MyDate(2022,12,4));
    set.add(e1);
    set.add(e2);
    set.add(e3);
    set.add(e4);
    set.add(e5);

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

二. 在List内去除重复数字值

@org.junit.Test
    public void test3(){
         List list=new ArrayList();
         list.add(new Integer(1));
         list.add(2);
         list.add(2);
         list.add(4);
         list.add(4);
         List list2=duplicateList(list);
         Iterator iterator=list2.iterator();
         while (iterator.hasNext()){
             System.out.println(iterator.next());
         }

}
public static List duplicateList(List list){
         HashSet set=new HashSet();
         set.addAll(list);
        return new ArrayList(set);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值