对象的比较方法
当我们编写的代码为简单的基本数据类型类型时,可以用 == 或者 equals 来进行对象的比较,而当随着代码难度的提高,我们使用基本数据类型的情况也随之减少,大部分情况下我们使用的都是引用数据类型,在这种情况下,我们就不能简单的使用 == 或者 equals 来进行比较了,在这里简单介绍四种进行对象比较的方法。
1、重写基类equals方法
例如:
class Student{stu_id,score,name}
s1 = new Student("zs",100,1);
s2 = new Student("lisi",100,2);
打印:s1.equals(s2); true
class Student{
private String name;
private int score;
private int stu_id;
public Student(String name,int score,int stu_id){
this.name = name;
this.score = score;
this.stu_id = stu_id;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Student){
Student student = (Student)obj;
if(this.score == student.score){
return true;
}
}
return false;
}
}
public class TestDemo {
public static void main(String[] args) {
Student s1 = new Student("zs",100,1);
Student s2 = new Student("lisi",100,2);
System.out.println(s1.equals(s2));
}
}
运行结果:
需要注意的是,我们在重写equals方法后,最好是将hashCode方法一起重写,原因可以参考:https://www.cnblogs.com/yuxiaole/p/9570850.html
2、实现接口中的方法
Comparable -> compareTo() 内比较器
class Student implements Comparable<Student>{
private String name;
private int score;
private int stu_id;
public Student(String name, int score, int stu_id) {
this.name = name;
this.score = score;
this.stu_id = stu_id;
}
@Override
public int compareTo(Student o) {
if(this.score > o.score){
return 1;
}else if(this.score == o.score){
return 0;
}else {
return -1;
}
}
@Override
public String toString() {
return name;
}
}
public class TestDemo {
public static void main(String[] args) {
Student s1 = new Student("zs",100,1);
Student s2 = new Student("lisi",90,2);
if(s1.compareTo(s2) > 0){
System.out.println(s1.toString()+"排名靠前");
}else if(s1.compareTo(s2) == 0){
System.out.println("并列");
}else {
System.out.println(s2.toString()+"排名靠前");
}
}
}
运行结果:
3、Comparator -> Compara()方法
外比较器
这种方法有两种写法,运行结果相同(其原理为匿名内部类)
写法一:
class People{
private String name;
private int age;
public People(String name,int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
}
class Compara implements Comparator<People> {
@Override
public int compare(People o1, People o2) {
if(o1.getAge() > o2.getAge()){
return 1;
}else if(o1.getAge() == o2.getAge()){
return 0;
}else {
return -1;
}
}
}
public class TestDemo{
public static void main(String[] args) {
Compara compara = new Compara();
People people1 = new People("zs",25);
People people2 = new People("lisi",26);
int result = compara.compare(people1,people2);
if(result > 0){
System.out.println("zs");
}else if(result == 0){
System.out.println("相等");
}else {
System.out.println("lisi");
}
}
}
写法二:
class People{
private String name;
private int age;
public People(String name,int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
}
public class TestDemo{
public static void main(String[] args) {
Comparator<People> comparator = new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
if(o1.getAge() > o2.getAge()){
return 1;
}else if(o1.getAge() == o2.getAge()){
return 0;
}else {
return -1;
}
}
};
People people1 = new People("zs",25);
People people2 = new People("lisi",26);
int result = comparator.compare(people1,people2);
if(result > 0){
System.out.println("zs");
}else if(result == 0){
System.out.println("相等");
}else {
System.out.println("lisi");
}
}
}
运行结果:
Eg:
class People implements Comparable<People>{
static class Company{
private String ComName;
private int salary;
public Company(String ComName,int salary){
this.ComName = ComName;
this.salary = salary;
}
}
private Company company;
private String name;
private int age;
public People(String name,int age,String ComName,int salary){
this.name = name;
this.age = age;
company = new Company(ComName,salary);
}
@Override
public int compareTo(People o) {
if(this.company.salary > o.company.salary){
return 1;
}
else if(this.company.salary == o.company.salary){
if(this.age > o.age){
return 1;
}
else if(this.age == o.age){
return 0;
}
else {
return -1;
}
}
else{
return -1;
}
}
@Override
public String toString() {
return name;
}
}
public class TestDemo{
public static void main(String[] args) {
People people1 = new People("zs",25,"abc",20000);
People people2 = new People("lisi",26,"abc",20000);
if(people1.compareTo(people2) > 0){
System.out.println(people1+"工资高");
}else if(people1.compareTo(people2) == 0){
System.out.println("工资相同");
}else {
System.out.println(people2+"工资高");
}
}
}
运行结果:
4、对象克隆(对象拷贝):Cloneable -> clone()方法
- 重写clone()方法
- 如果当前类中存在引用数据类型,修改该类中clone方法(单独对引用数据类型进行克隆)
- 内存是否共享
- 深拷贝:没有内存共享
- 浅拷贝:有内存共享
class Address implements Cloneable{
int home_id;
public Address(int home_id) {
this.home_id = home_id;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class People implements Cloneable{
String name;
int age;
Address address;
public People(String name, int age,int home_id) {
this.name = name;
this.age = age;
address = new Address(home_id);
}
@Override
protected Object clone() throws CloneNotSupportedException {
People o = (People)super.clone();
o.address = (Address)this.address.clone();
return o;
}
}
public class TestDemo{
public static void main(String[] args) {
People p1 = new People("zs",10,111);
People p2 = null;
try {
p2 = (People)p1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(p1.age+" "+p2.age);
}
}
运行结果: