java 对象相等判断 equals comparable comparator区别

时间:2023-01-02 10:36:25
-->java 判断两个对象引用变量是否相等(包括内存地址和对象值)
-->对于对象的理解(对象new多个就存在多少 "="也只是赋予了可以调用这个对象的操作)
    --> "==" (!!基本类:对象值  对象:内存地址(即所谓的对象引用))     --> equals.(Object o) (若对象没覆盖也是内存地址,已覆盖的则比较的是对象值)
        -->java api已覆盖的equals的方法包括
            -->String
            -->Date
            -->Java包装类(就是对基本类的转换为了对象处理)

-->关于如何覆盖Object的equals方法
public boolean equals(Object o){ if(this == o) return true; if(!o instanceof Xudaolong) return false; final Xudaolong other = (Xudaolong)o; if(this.getName().equals(other.getName())) return true; else  return false;}

-->顺便提一下comparable(可排序的接口,意味着该类支持排序)(PS:able结尾的一般是接口,需要实现)
import java.util.*; public interface Comparable<T> { public int compareTo(T o);}
    -->即可对已实现该接口的类的对象的List 和 Array 分别进行  Collections.sort(或 Arrays.sort)
    -->对于compareTo(),是该接口下唯一需要实现的方法,返回值是-1,0,1
    -->api中已实现该接口的类:String、Integer、Date、Time等等。

-->comparator(比较器接口):类的次序而并非类的排序
package java.util; public interface Comparator<T> {  int compare(T o1, T o2);  boolean equals(Object obj);}

-->总结 comparable是对对象内部进行加工, comparator 是在外部new新方法进行对对象的操作
-->实践代码
public class Person implements Comparable<Person> {private int age;private String name;public Person(String string, int i) {this.name = string;this.age = i;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;} @Overridepublic int compareTo(Person o) {return String.valueOf(this.age).compareTo(String.valueOf(o.age));} @Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;} @Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Person other = (Person) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;} @Overridepublic String toString() {return "Person [age=" + age + ", name=" + name + "]";}}
-->测试代码
 import java.util.ArrayList;import java.util.Collections;import java.util.Comparator; import bean.Person; public class java_comparable {public static void main(String[] args) {ArrayList<Person> persons = new ArrayList<Person>(); persons.add(new Person("x", 5));persons.add(new Person("d", 2));persons.add(new Person("l", 1));persons.add(new Person("c", 4));System.out.println(persons);Collections.sort(persons);System.out.println(persons);Collections.sort(persons, new comparator_asc());System.out.println(persons);Collections.sort(persons, new comparator_desc());System.out.println(persons);}//升序private static class comparator_asc implements Comparator<Person> {@Overridepublic int compare(Person o1, Person o2) {return o1.getAge() - o2.getAge();}}//降序private static class comparator_desc implements Comparator<Person> {@Overridepublic int compare(Person o1, Person o2) {return o2.getAge() - o1.getAge();}}}
结果:
java 对象相等判断 equals comparable comparator区别
java 对象相等判断 equals comparable comparator区别