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

在Java中使用equals方法查找相等对象,java,equals,hashcode,Java,Equals,Hashcode,我有一个对象列表,其中包含名称、地址行1、地址行2、城市、人员匹配属性列表。通过重写equals和hashcode方法,我发现基于地址行1、地址行2和城市(而不是名称)的相等性。现在我想获取对象匹配的人的姓名,并将其存储在listOfPeopleMatched中。例如:[“瓦尔”,“阿什什”,“史蒂夫”,“亚历克斯]]。如何仅在equals方法中实现这一点 public class Person { private String name; private String add

我有一个对象列表,其中包含名称、地址行1、地址行2、城市、人员匹配属性列表。通过重写equals和hashcode方法,我发现基于地址行1、地址行2和城市(而不是名称)的相等性。现在我想获取对象匹配的人的姓名,并将其存储在listOfPeopleMatched中。例如:[“瓦尔”,“阿什什”,“史蒂夫”,“亚历克斯]]。如何仅在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;
    }
}
公共类人物{
私有字符串名称;
私有字符串地址行1;
私有字符串地址行2;
私人城市;
private List listOfPeopleMatched=
新的ArrayList();
公共字符串getName(){
返回名称;
}
公共列表getListOfPeopleMatched(){
已匹配人员的返回列表;
}
public void setListOfPeopleMatched(listOfPeopleMatched){
this.listOfPeopleMatched=listOfPeopleMatched;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getAddressLine1(){
返回地址行1;
}
公共无效setAddressLine1(字符串addressLine1){
this.addressLine1=addressLine1;
}
公共字符串getAddressLine2(){
返回地址行2;
}
公共无效setAddressLine2(字符串addressLine2){
this.addressLine2=addressLine2;
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
this.city=城市;
}
公众人物(字符串名称,字符串地址Line1,
字符串地址行2,字符串城市){
超级();
this.name=名称;
this.addressLine1=addressLine1;
this.addressLine2=addressLine2;
this.city=城市;
}
@凌驾
公共字符串toString(){
return“Person[name=“+name+
“,addressLine1=“+
addressLine1+”,addressLine2=“+
地址行2+“,城市=”
+城市+“]”;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果+((addressLine1==null)?
0:addressLine1.hashCode());
结果=prime*result+((addressLine2==null)?
0:addressLine2.hashCode());
结果=素数*结果+((城市==null)?
0:city.hashCode());
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
人员其他=(人员)obj;
如果(addressLine1==null){
if(other.addressLine1!=null)
返回false;
}如果(!addressLine1.equals(other.addressLine1))
返回false;
如果(addressLine2==null){
if(other.addressLine2!=null)
返回false;
}如果(!addressLine2.equals(other.addressLine2))
返回false;
如果(城市==null){
if(other.city!=null)
返回false;
}else如果(!city.equals(other.city))
返回false;
返回true;
}
}

Person person1=新人(“Val”、“ABC”、“Shivaji Nagar”、“Pune”);
Person person2=新人(“Ashish”、“ABC”、“Shivaji Nagar”、“Pune”);
Person person3=新人(“Steve”、“MNO”、“Shivaji Nagar”、“Pune”);
Person person4=新人(“Alex”、“MNO”、“Shivaji Nagar”、“Pune”);
Set uniquePeople=newhashset();
唯一的人。添加(个人1);
唯一的人。添加(个人2);
唯一的人。添加(person3);
唯一的人。添加(个人4);
System.out.println(uniquePeople);

我认为您希望将地址数据封装在一个对象中。这样一来,一个人就不等于他的地址,你可以进行更细致的搜索。另外,您正以这种方式分离关注点

我在这里写了一个工作示例:

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 +
               '}';
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Objects;
导入java.util.stream.collector;
公共阶层人士{
公共静态void main(字符串[]args){
//我们在寻找什么
地址=新地址(“第三街123号”,“ohg东部”,“本市”);
//初始化
List persons=new ArrayList();
人员。添加(新人员(“吉姆”、“北纬56街123号”、“东ohg”、“本市”);
新增人员(新人员(“凯尔”、“北67街145号”、“东ohg”、“本市”);
新增(新人员(“Sam”、“12海滩大道”、“东ohg”、“本市”);
新增人员(新人员(“特雷西”、“第三街123号”、“东ohg”、“本市”);
人员。添加(新人员(“阿什利”、“第三街123号”、“ohg东部”、“本市”);
//搜寻
List people=persons.stream().filter(person->person.address.equals(address)).collect(Collectors.toList());
people.forEach(System.out::println);
}
字符串名;
地址;
公众人物(字符串名称,
字符串地址行1,
字符串地址行2,
(字符串城市){
this.name=名称;
this.address=新地址(addressLine1,
地址行2,
城市);
}
私有静态最终类地址{
字符串地址行1;
字符串地址行2;
字符串城市;
公共地址(字符串地址行1、字符串地址行2、字符串城市){
this.addressLine1=addressLine1;
this.addressLine2=addressLine2;
this.city=城市;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
地址=(地址)o;
返回Objects.equals(addressLine1,address.addressLine
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 +
               '}';
    }
}