gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:04:52 24 4
gpt4 key购买 nike

我有一个对象列表,其中包含名称、地址行 1、地址行 2、城市、listOfPeopleMatched 属性。我通过重写 equals 和 hashcode 方法找到基于地址 line1、地址 line2 和城市(而不是名称)的相等性。现在我想获取对象匹配的人员姓名并将其存储在 listOfPeopleMatched 中。例如:[[“Val”,“Ashish”],[“Steve”,“Alex”]]。仅在 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);

最佳答案

我认为您想将地址数据封装在一个对象中。这样,“人员”并不等于其地址,您可以进行更精细的搜索。另外,您可以通过这种方式分离关注点。

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

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 +
'}';
}
}

关于java - Java中使用equals方法查找相等的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58406073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com