简体   繁体   English

如何检查数组或列表中对象的特定字段是否唯一?

[英]How can I check if a specific fields of objects within an array or list are unique?

If I have a list of objects like this,如果我有这样的对象列表,

person = [
    a = {
        name = john
        address = bakerfield
        phone = 1234
    },
    b = {
        name = stacy
        address = cloverfield
        phone = 4567
    },
    .
    .
    .
    .
]

I want to return true if all object's name and address fields are unique.如果所有对象的名称和地址字段都是唯一的,我想返回 true。 It should return true if an object has same name but different address or vice versa.如果 object 具有相同的名称但不同的地址,则它应该返回 true,反之亦然。

I am fairly new to Java so having hard time figuring this one out.我对 Java 还很陌生,所以很难弄清楚这一点。 I tried implementing this with contains method but that didn't work.我尝试使用contains方法来实现它,但没有奏效。

Edit: fixed json formatting to object编辑:将 json 格式修复为 object

Edit 2: I was able to solve this problem using Comparator.comparing along with TreeSet<> .编辑 2:我能够使用Comparator.comparingTreeSet<>解决这个问题。

Firstly you need to write a proper equals (and hashCode also! Read the first link below) method in your Person class that will compare all fields to understand whether the instance is duplicate or not.首先,您需要在您的Person class 中编写一个正确的equals (和hashCode也是!阅读下面的第一个链接)方法,该方法将比较所有字段以了解实例是否重复。

When you will do this you can use a feature of java Set collection that guarantees that you will have only one instance of the object inside (the duplicate will be "ignored" when added) and compare the size of your initial array/list and the set created on this array/list当您执行此操作时,您可以使用 java Set集合的功能,该集合保证您将只有一个 object 实例(添加时重复项将被“忽略”)并比较初始数组/列表的大小和在此数组/列表上创建的集合

List<Person> people = someDeserializeJsonMethod();
return people.size() == new HashSet<>(people).size();

Read more:阅读更多:

When you say you have a list.当你说你有一个清单。 Do you mean you have: List<Person> person;你的意思是你有: List<Person> person; ? ?

Because you say you are trying to use contains .因为您说您正在尝试使用contains Contains compares objects, not values of objects.包含比较对象,而不是对象的值。 For example if I had an ArrayList of strings.例如,如果我有一个 ArrayList 字符串。

ArrayList<String> colors = new ArrayList<>;

And I had some strings like:我有一些字符串,例如:

String a = "red";
String b = "blue";
String c = "green";
String d = "green";

Then added a,b,c to the ArrayList, but left out d.然后将 a,b,c 添加到 ArrayList,但省略了 d。

colors.add(a);
colors.add(b);
colors.add(c);

With the objects now in the arraylist.对象现在位于 arraylist 中。 I can use compare like this:我可以像这样使用比较:

colors.contains(a)

This will return true because the object 'a' is in the ArrayList.这将返回 true,因为 object 'a' 在 ArrayList 中。 But if you try colors.contains(d) it'll return false.但是,如果您尝试colors.contains(d)它将返回 false。 Even though the value of 'c' is 'green' which is the same value as 'd'.即使“c”的值是“green”,它与“d”的值相同。 It isn't the same object.它与 object 不同。 Because '.contains()' is just comparing objects and not the value of the object.因为 '.contains()' 只是比较对象而不是 object 的值。

If you want to compare the values of a list/arrayList than you will have to a For loop.如果要比较列表/数组列表的值,则必须使用 For 循环。

public boolean comparePerson(ArrayList persons, Person person) { //persons is your main list. public boolean comparePerson(ArrayList persons, Person person) { //persons 是您的主要列表。 And person is a single person that you want to see if they are in the ArrayList.而 person 是一个人,你想看看他们是否在 ArrayList 中。

for (Person p: persons) {
    if (person.name == p.name) {
        if (person.address == p.address) {
        return true;
        }
    }
}
return false;

} }

Optionally this is why having an Id field is useful.可选地,这就是为什么有一个 Id 字段是有用的。 You'll only need to compare Ids and not multiple fields for a confident match.您只需要比较 Id 而不是多个字段即可获得可靠匹配。

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

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