简体   繁体   English

Java中使用equals方法查找相等对象

[英]Finding equal objects using equals method in Java

I have a list of objects which has name, address line1, address line2, city, listOfPeopleMatched properties.我有一个对象列表,其中包含名称、地址 line1、地址 line2、城市、listOfPeopleMatched 属性。 I am finding equality based on address line1, address line2 and city (not name) by overriding equals and hashcode methods.我通过覆盖 equals 和 hashcode 方法找到基于地址 line1、地址 line2 和城市(不是名称)的相等性。 Now I want to get the name of people whose objects matches and store them in listOfPeopleMatched.现在我想获取对象匹配的人的姓名并将它们存储在 listOfPeopleMatched 中。 For example: [["Val","Ashish"], ["Steve","Alex"]].例如:[["Val","Ashish"], ["Steve","Alex"]]。 How can this be done in equals method only?如何仅在 equals 方法中完成?

public class Person {

    private String name;
    private String addressLine1;
    private String addressLine2;
    private String city;
    private List<List<String>> listOfPeopleMatched = 
                                 new ArrayList<List<String>>();

    public String getName() {
        return name;
    }

    public List<List<String>> getListOfPeopleMatched() {
        return listOfPeopleMatched;
    }

    public void setListOfPeopleMatched(List<List<String>> listOfPeopleMatched) {
        this.listOfPeopleMatched = listOfPeopleMatched;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Person(String name, String addressLine1, 
            String addressLine2, String city) {
        super();
        this.name = name;
        this.addressLine1 = addressLine1;
        this.addressLine2 = addressLine2;
        this.city = city;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + 
                        ", addressLine1=" + 
                        addressLine1 + ", addressLine2=" + 
                        addressLine2 + ", city="
                + city + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((addressLine1 == null) ? 
                0 : addressLine1.hashCode());
        result = prime * result + ((addressLine2 == null) ? 
                0 : addressLine2.hashCode());
        result = prime * result + ((city == null) ? 
                0 : city.hashCode());
        return result;
    }

    @Override
    public 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 (addressLine1 == null) {
            if (other.addressLine1 != null)
                return false;
        } else if (!addressLine1.equals(other.addressLine1))
            return false;
        if (addressLine2 == null) {
            if (other.addressLine2 != null)
                return false;
        } else if (!addressLine2.equals(other.addressLine2))
            return false;
        if (city == null) {
            if (other.city != null)
                return false;
        } else if (!city.equals(other.city))
            return false;
        return true;
    }
}

Person person1 = new Person("Val", "ABC", "Shivaji Nagar", "Pune");
Person person2 = new Person("Ashish", "ABC", "Shivaji Nagar", "Pune");
Person person3 = new Person("Steve", "MNO", "Shivaji Nagar", "Pune");
Person person4 = new Person("Alex", "MNO", "Shivaji Nagar", "Pune");

Set<Person> uniquePeople = new HashSet<>();
uniquePeople.add(person1);
uniquePeople.add(person2);
uniquePeople.add(person3);
uniquePeople.add(person4);

System.out.println(uniquePeople);

I think you want to encapsulate your Address data in an object.我认为您想将您的地址数据封装在 object 中。 That way a Person isn't equal to it's address and you can do more granular searching.这样一个 Person 不等于它的地址,您可以进行更精细的搜索。 Plus you are separating the concerns this way.另外,您以这种方式分离关注点。

I wrote up a working example here:我在这里写了一个工作示例:

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Person {

    public static void main(String[] args) {


        // what we're searching for
        Address address = new Address("123 N 3rd st", "east ohg", "this-city");

        // init
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Jim", "123 N 56 st", "east ohg", "this-city"));
        persons.add(new Person("Kyle", "145 N 67th st", "east ohg", "this-city"));
        persons.add(new Person("Sam", "12 beach av", "east ohg", "this-city"));
        persons.add(new Person("Tracy", "123 N 3rd st", "east ohg", "this-city"));
        persons.add(new Person("Ashley", "123 N 3rd st", "east ohg", "this-city"));


        // search
        List<Person> people = persons.stream().filter(person -> person.address.equals(address)).collect(Collectors.toList());

        people.forEach(System.out::println);


    }

    String name;
    Address address;

    public Person(String name,
                  String addressLine1,
                  String addressLine2,
                  String city) {
        this.name = name;
        this.address = new Address(addressLine1,
                                   addressLine2,
                                   city);
    }

    private static final class Address {
        String addressLine1;
        String addressLine2;
        String city;


        public Address(String addressLine1, String addressLine2, String city) {
            this.addressLine1 = addressLine1;
            this.addressLine2 = addressLine2;
            this.city = city;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Address address = (Address) o;
            return Objects.equals(addressLine1, address.addressLine1) &&
                   Objects.equals(addressLine2, address.addressLine2) &&
                   Objects.equals(city, address.city);
        }

        @Override
        public int hashCode() {
            return Objects.hash(addressLine1, addressLine2, city);
        }


        @Override
        public String toString() {
            return "Address{" +
                   "addressLine1='" + addressLine1 + '\'' +
                   ", addressLine2='" + addressLine2 + '\'' +
                   ", city='" + city + '\'' +
                   '}';
        }
    }


    @Override
    public String toString() {
        return "Person{" +
               "name='" + name + '\'' +
               ", address=" + address +
               '}';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM