泛型经典例题(包含匿名内部类,sort方法,泛型使用,ArrayList集合)

 

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

public class GenericWork {
    public static void main(String[] args) {

        List<Employee> list = new ArrayList<>();
        list.add(new Employee("Bill",5000,new Mydate(2004,3,9)));
        list.add(new Employee("Satily",8000,new Mydate(2006,9,24)));
        list.add(new Employee("Martin",11000,new Mydate(2001,5,2)));
        System.out.println(list);
        list.sort(new Comparator<Employee>() {
            @Override//使用匿名内部类重写接口Compartor,传入字符就判断ASCII码值,数字就判断大小,再根据冒泡法排序
            public int compare(Employee o1, Employee o2) {
                if(!(o1 instanceof Employee && o2 instanceof Employee)) {
                    return 0;
                }
                if(o1.getName().compareTo(o2.getName()) != 0){
                    int i = o1.getName().compareTo(o2.getName());
                    return i;
                }
                if(o1.getBirthday().getYear() - o2.getBirthday().getYear() != 0){
                    int i1 = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                    return i1;
                }
                if(o1.getBirthday().getMonth() - o2.getBirthday().getMonth() != 0){
                    return o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                }
                if(o1.getBirthday().getDay() - o2.getBirthday().getDay() != 0){
                    return o1.getBirthday().getDay() - o2.getBirthday().getDay();
                }
                return 0;
            }
        });
        System.out.println(list);
    }
}

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 "\nEmployee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class 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 "Mydate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值