如何在Java 8中比较两个对象

时间:2022-04-11 02:17:44

Lets take an example you have two employee object having same values as follows.

让我们举个例子,你有两个员工对象具有如下相同的值。

Employee employee1 = new Employee(1001, "Sam", 20000);
Employee employee2 = new Employee(1001, "Sam", 20000);
if(doCompareEmployees(employee1, employee2)){
    System.out.println("Both employee objects are same.");
}else{
    System.out.println("Both employee objects are not same.");
}

//Here is compare method using java 8.

private boolean doCompareEmployees(Employee employee1, Employee employee2) {
int returnValue = Comparator.comparing(Employee::getID)
    .thenComparing(Employee::getName)
    .thenComparing(Employee::getSalary)
    .compare(employee1, employee2);
    if (returnValue != 0){
        return false;
    }   
    return true;
}

I would like know about, is there any other better approach to compare objects in Java 8 ?

我想知道,有没有更好的方法来比较Java 8中的对象?

2 个解决方案

#1


2  

If you do not want to define an ordering on your objects, normally you would not write a Comparator.

如果您不想在对象上定义排序,通常不会编写Comparator。

The typical way to define equality for a class, is to define both an equals() and a hashCode() method. To implement hashCode(), Objects.hash() can help (present since Java 7).

定义类的相等性的典型方法是定义equals()和hashCode()方法。要实现hashCode(),Objects.hash()可以提供帮助(从Java 7开始呈现)。

public int hashCode() {
    return Objects.hash(id, name, salary);
}

public boolean equals(Object o) {
    if (o == this) return true;
    if (o == null || o.getClass() != getClass()) return false;
    Employee e = (Employee) o;
    return id == e.id && salary == e.salary && Objects.equals(name, e.name);
}

Although lambda expressions allow to write very elegant code in some cases, they are not the best solution for each problem.

尽管lambda表达式允许在某些情况下编写非常优雅的代码,但它们并不是解决每个问题的最佳解决方案。

#2


0  

You can check this LambdaEquals utility. However, as a good habit, you should stick to the equals overriding for the best performance. Overriding the "equals" method could be faster than Comparator.comparing.

您可以检查此LambdaEquals实用程序。但是,作为一个好习惯,你应该坚持等于压倒最佳表现。覆盖“equals”方法可能比Comparator.comparing更快。

Below is the same example of override as Hoopje provided, just slightly differs.

下面是与Hoopje提供的覆盖相同的例子,略有不同。

In the Employee class override the equals method:

在Employee类中重写equals方法:

public class Employee {
.....
.....
@Override
public boolean equals(Object o) {
     if(super.equals(o)) {
         return true;
     }
    if(!(o instanceof Employee)) {
        return false
    }

    Employee otherEmployee = (Employee) o;

    return 
        id == otherEmplyee.getId &&
        name == otherEmplyee.getName() &&
        salary == otherEmplyee.getSalary;

}


//Use the equal method
if(emp1.equals(emp2) {
    //do something
}

#1


2  

If you do not want to define an ordering on your objects, normally you would not write a Comparator.

如果您不想在对象上定义排序,通常不会编写Comparator。

The typical way to define equality for a class, is to define both an equals() and a hashCode() method. To implement hashCode(), Objects.hash() can help (present since Java 7).

定义类的相等性的典型方法是定义equals()和hashCode()方法。要实现hashCode(),Objects.hash()可以提供帮助(从Java 7开始呈现)。

public int hashCode() {
    return Objects.hash(id, name, salary);
}

public boolean equals(Object o) {
    if (o == this) return true;
    if (o == null || o.getClass() != getClass()) return false;
    Employee e = (Employee) o;
    return id == e.id && salary == e.salary && Objects.equals(name, e.name);
}

Although lambda expressions allow to write very elegant code in some cases, they are not the best solution for each problem.

尽管lambda表达式允许在某些情况下编写非常优雅的代码,但它们并不是解决每个问题的最佳解决方案。

#2


0  

You can check this LambdaEquals utility. However, as a good habit, you should stick to the equals overriding for the best performance. Overriding the "equals" method could be faster than Comparator.comparing.

您可以检查此LambdaEquals实用程序。但是,作为一个好习惯,你应该坚持等于压倒最佳表现。覆盖“equals”方法可能比Comparator.comparing更快。

Below is the same example of override as Hoopje provided, just slightly differs.

下面是与Hoopje提供的覆盖相同的例子,略有不同。

In the Employee class override the equals method:

在Employee类中重写equals方法:

public class Employee {
.....
.....
@Override
public boolean equals(Object o) {
     if(super.equals(o)) {
         return true;
     }
    if(!(o instanceof Employee)) {
        return false
    }

    Employee otherEmployee = (Employee) o;

    return 
        id == otherEmplyee.getId &&
        name == otherEmplyee.getName() &&
        salary == otherEmplyee.getSalary;

}


//Use the equal method
if(emp1.equals(emp2) {
    //do something
}