我有一个对象列表,该对象具有名称,地址第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 uniquePeople = new HashSet<>(); uniquePeople.add(person1); uniquePeople.add(person2); uniquePeople.add(person3); uniquePeople.add(person4);
System.out.println(uniquePeople);
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 +
'}';
}
想将地址数据封装在一个对象中。这样,“Person”不等于其地址,你可以进行更精细的搜索。另外,你可以通过这种方式分离问题。
我在这里写了一个示例:
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 +
'}';
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。