栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 

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

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

        List 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() {
            @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 +
                '}';
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号