开发者社区> 问答> 正文

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

我有一个对象列表,该对象具有名称,地址第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);

展开
收起
小六码奴 2019-10-16 18:14:50 10714 0
2 条回答
写回答
取消 提交回答
  • 下一站是幸福
    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 +
               '}';
    }
    
    2020-04-27 23:16:47
    赞同 展开评论 打赏
  • 想将地址数据封装在一个对象中。这样,“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 +
               '}';
    }
    

    }

    2019-10-16 18:35:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载