Java_Generic

题目:

定义Employee类,其中birthday为MyDate类的对象;定义类该有的构造器,getter和setter及toString方法;创建3个Employee类的对象,并加入到集合ArrayList(泛型定义),对元素遍历输出.排序方式:调用ArrayList的sort方法,传入Comparator对象(使用泛型),先按照name排序,name相同则按照出生年份排序,相同则生日日期 

package com.generic;

import java.util.ArrayList;
import java.util.Comparator;

@SuppressWarnings({"all"})
public class GenericExercise {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("jack",2000.45,new MyDate(2001,3,1)));
        employees.add(new Employee("mark",3200.98,new MyDate(1998,2,4)));
        employees.add(new Employee("jack",5600.98,new MyDate(1997,10,11)));
        //排序
        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee e1, Employee e2) {
                //先按name排序,相同则按生日排序
                //先对传入的参数进行验证
                if (!(e1 instanceof Employee && e2 instanceof Employee)){
                    return 0;
                }
                if (e1 == null && e2 == null) {
                    return 0;
                }
                int i = e1.getName().compareTo(e2.getName());
                if(i != 0 ){
                    return i;
                }
                return e1.getBirthday().compareTo(e2.getBirthday());
            }
        });
        System.out.println(employees);
    }
}
class Employee{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

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

    @Override
    public String toString() {
        return "\n" +
                "姓名='" + name + '\'' +
                ", 薪水=" + sal +
                ", 生日:" + birthday +
                '}';
    }
}
class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    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
                "年=" + year +
                ", 月=" + month +
                ", 日=" + day ;
    }

    @Override
    public int compareTo(MyDate o) {
        //名字相同,比较年
        /*
         employees.add(new Employee("jack",2000.45,new MyDate(2001,3,1)));  就是下面的 year
         employees.add(new Employee("mark",3200.98,new MyDate(1998,2,4)));   下面的O.getyear()

        * */
        int yearminus = year- o.getYear();
        if (yearminus != 0){
            return yearminus;
        }
        //年相同,比较月
        int monthminus = month- o.getMonth();
        if (monthminus != 0){
            return monthminus;
        }
        //月相同,比较日
        return day- o.getDay();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值