Bigdata Development Java_Study_07(equals,HashCode和compare Date)

来源:互联网 发布:微信清死粉软件安卓版 编辑:程序博客网 时间:2024/06/08 02:59

equals和HashCode

// HashSet 相同的元素只会添加一个,靠的是 hashCode 和 equals 方法        // 引用类型 == 比较的是地址,equals 默认比较的也是地址        // 可以重写 equals 方法实现值的比较,比如:String        // 重写 equals 必须同时重写 hashCoe,是两者保持一致        // equals 相等,hashCode 相等,equals 不相等,hashCode 也不相等        //下面为在People类中重写了HashCode和equals方法。public class People {    private String name;    private int age;    // alt + shift + s    public People() {        super();    }    public People(String name, int age) {        super();        this.name = name;        this.age = age;    }    // 系统自动生成的对象的 唯一编号    // 1. equals 相等的两个的对象 hashCode 必须一致    // 系统为了提高效率,HashSet 等判断两个对象相同的时候,会先比较 hashCode    // 如果一致,调用 equals 进行比较,如果不一致,那就认为不相同,不在调用 equals    // 重写 equals 方法必须重写 hashCode 保证值相等对象,hashCode 也相等    //    // 2. hashCode 相等的 equals 不一定相等    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + age;        result = prime * result + ((name == null) ? 0 : name.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        // 可以传入任意对象        if (this == obj) // 同一个对象进行比较肯定是相同的return true; // 如果没有 {} 那么只有紧跟着 if 的下行一行代码在 if 判断内        if (obj == null) // 如果参数为 null,肯定不相等            return false;if (!(obj instanceof People)) // instance 实例化,创建对象return false; // instanceof 判断是否是某个类的对象// 判断 obj 是否是 People 或其子类的对象                            // true 是,false 不是                            // ! 表示取反                            // 如果类型不一致,肯定不相等        // 排除所有错误情况,进行值的比较        People other = (People) obj; // 强制转换为 People 类型// obj 的编译时类型为 Object,决定了我们写代码的时候能够调用哪些属性和方法// 调用不了 People 类的 name 和 age 属性// 通过强制转换,把编译时类型改为 People,就可以调用 People 的属性和方法了        // 比较两个对象属性的值,是否相等        // 基本类型使用 == 进行值想等的比较        if (age != other.age)            return false;// 引用类型使用 equals 进行值相等的比较,防止出现地址不同,值相等的情况,String        // if (name != other.name)        // return false;        if (name == null) { // 如果 name == null,调用 equals 会产生 空指针 异常if (other.name != null) // 一个为 null,另一个不为 null                return false;        } else if (!name.equals(other.name)) // 取反,不相等的时候返回 false            return false;return true; // 如果能够执行到这里,说明 age 和 name 都相同,返回 true    }    @Override    public String toString() {        return "People [name=" + name + ", age=" + age + "]";    }    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;    }}

compare

public class People implements Comparable<People>{    // 使用枚举    public static PeopleSortEnum sortType;     private String name;    private int age;    private double height;    private double weight;    public People() {        super();    }    public People(String name, int age, double height, double weight) {        super();        this.name = name;        this.age = age;        this.height = height;        this.weight = weight;    }    @Override    public int compareTo(People o) {        switch (People.sortType) {        case HEIGHT: {            // 按照身高排序            if (this.height > o.height) {                return 1;            }else if (this.height < o.height) {                return -1;            }else {                return 0;            }        }            // Unreachable code            // break; // return 后边的代码都不执行        case WEIGHT : {            // 按照体重排序            if (this.weight > o.weight) {                return 1;            }else if (this.weight < o.weight) {                return -1;            }else {                return 0;            }        }        case AGE: {            return this.age - o.age;        }        case NAME: {            // 按照名字排序,就返回两个名字的比较结果            return this.name.compareTo(o.name);        }        default:            return 0;        }    }    @Override    public String toString() {        return "People [name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight + "]";    }    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 double getHeight() {        return height;    }    public void setHeight(double height) {        this.height = height;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }}//---------------------------------------------------------public enum PeopleSortEnum {    HEIGHT,    WEIGHT,    AGE,    NAME;}//---------------------------------------------------------public class Test {    public static void main(String[] args) {        People p1 = new People("张三", 28, 1.75, 80);        People p2 = new People("李四", 30, 1.85, 90);        People p3 = new People("王五", 24, 1.65, 60);        People p4 = new People("赵六", 29, 1.80, 70);        ArrayList<People> al1 = new ArrayList<People>();        al1.add(p1);        al1.add(p2);        al1.add(p3);        al1.add(p4);        // 按照升序或者降序排序        // 按照身高或者体重排序        // 1. People 实现 Comparable 接口,添加 compareTo 方法        // 2. People 增加 static 变量 flag,设置 People 的排序方式        // 3. 根据 flag 属性实现  compareTo 方法        // 按照身高排序        People.sortType = PeopleSortEnum.HEIGHT;        People.sortType = PeopleSortEnum.WEIGHT;        People.sortType = PeopleSortEnum.AGE;        People.sortType = PeopleSortEnum.NAME;        // People.flag = false;// 类 只有 实现了 Comparable 接口,说明比较方式,才能进行排序        Collections.sort(al1);        for (People people : al1) {            System.out.println(people);        }    }}

Date

public class Test {    public static void main(String[] args) {        // 日期时间         // JDK 1.8 以后提供的新的时间相关的类        // 获取当前的年月日        LocalDate date = LocalDate.now();        System.out.println(date);        // 获取当前的时分秒,精确到毫秒        LocalTime time = LocalTime.now();        System.out.println(time);        // 剔除毫秒        LocalTime time2 = LocalTime.now().withNano(0);        System.out.println(time2);        // 获取年月日时分秒        LocalDateTime dateTime = LocalDateTime.now().withNano(0);        System.out.println(dateTime);    }}//------------------------------------------------------public class Test2 {    public static void main(String[] args) {        // 2014-12-02        // 把字符串转换为 LocalDate,必须使用下面的格式        LocalDate date1 = LocalDate.parse("2014-12-02");        System.out.println(date1);        System.out.println(date1.getYear() + " 年");        System.out.println(date1.getMonth() + " 月");        System.out.println(date1.getDayOfMonth() + " 日");        System.out.println("这一年的第:" + date1.getDayOfYear());        System.out.println("周几:" + date1.getDayOfWeek());        System.out.println("32 天后是:" + date1.plusDays(32)); // +        System.out.println("32 天前是:" + date1.minusDays(32)); // -\        date1.plusMonths(2);        date1.plusWeeks(2);        date1.plusYears(2);        date1.minusMonths(2);        date1.minusWeeks(2);        date1.minusYears(2);        date1.isLeapYear();        // LocalTime 和 LocalDateTime 使用和上边是一样的    }}//------------------------------------------------------public class Test3 {    public static void main(String[] args) {        LocalDate date = LocalDate.now();        // 设置日期的自定义输出格式        // y 年 M 月 d 日        // 这里写什么就输出什么,把 字母 替换为 数字        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("今天是:MM月dd日yyyy年");        // 把日期转换为指定格式的字符串        System.out.println(dtf.format(date));        LocalDate date2 = LocalDate.parse("今天是:07月25日2019年", dtf);        System.out.println(date2);        // LocalTime 和 LocalDateTime 也是一样的操作        // H m s    }}

这里写图片描述

原创粉丝点击