简体   繁体   English

检查两个不同的ArrayList是否相等

[英]Check if two different ArrayLists are Equal

I have an Array List of type String and an Array List of type Person. 我有一个String类型的数组列表和一个Person类型的数组列表。 Where Person is an Object that only contains a String that holds the name. 其中Person是一个仅包含持有名称的字符串的对象。

ArrayList<Person> people = new ArrayList<Person>();
ArrayList<String> names = new ArrayList<String>();

Let's say I do this, 假设我这样做

names.add("Josh");
people.add(new Person("Josh");

Assuming that creating a new Person Object will set the name to be "Josh", and assuming that the Person class has a get method for the name. 假设创建一个新的Person Object会将名称设置为“ Josh”,并假设Person类具有该名称的get方法。

Is there a way to check if the names Array List contains the Person who is named Josh. 有没有一种方法可以检查名称数组列表是否包含名为Josh的人员。

The only thing I can think of is this, 我唯一能想到的就是

for(int i = 0; i < names.size(); i++){
    if(names.get(i).equals(people.get(i).getName)){
        return true;
    } // end if     
} // end for

Now if the Person Array List and the names Array List contain more than one element, How do I check if a String in the names Array List contains a name of one of the Persons? 现在,如果“人员数组列表”和名称“数组列表”包含多个元素,如何检查名称数组列表中的字符串是否包含“人员”之一的名称? Would I use this? 我会用这个吗?

 for(int i = 0; i < names.size(); i++){
    for(int j = 0; j < people.size(); j++){
        if(names.get(i).equals(people.get(j).getName)){
            return true;
        } // end if
    }  // end nested for    
 } // end for

Or would this not even be possible, since the Array Lists contain different Objects? 还是因为数组列表包含不同的对象,这甚至不可能吗?

Yes, you could implement it the way you suggest. 是的,您可以按照建议的方式实施它。 To make it more concise, you could also use the for-each construct and switch the loops ( names gets looped within contains ): 为了更简洁,您还可以使用for-each构造并切换循环( namescontains循环):

for (Person p : people) {
    if (names.contains(p.getName()) {
       return true;
    }
 }

In Java 8 you can do this using Streams.anyMatch() : 在Java 8中,您可以使用Streams.anyMatch()做到这一点:

Return true if there is any Person in people whose name is in names : 返回true ,如果有任何Personpeople的名字是names

people.stream().anyMatch(person -> names.contains(person.getName());

Note that this would be much faster if names were a Set<String> instead of a List<String> . 请注意,如果namesSet<String>而不是List<String> ,这会更快。

First check that the lists are the same length. 首先检查列表长度是否相同。
Then check that each element in one is contained in the other. 然后检查一个元素中的每个元素是否包含在另一个元素中。 Of course this assumes that names are unique among the lists. 当然,这假定名称在列表中是唯一的。

if (people.size () != names.size ()){
    return false;
}
for(Person p : people) {
    if(!names.contains(p.getName())){
         return false;
    }
}
return true;

Possible Solutions: 可能的解决方案:

Solution 1 : Simplest approach 解决方案1 :最简单的方法

public boolean check(List<Person> people, List<String> names) {
    for(Person person : people) {
        if (names.contains(p.getName()) {
            return true;
        }
    }
}

NOTE: This is going to give a bad performance if there are a lot of entries in the list 'names', as it might be an NxN comparison (in the worst case) 注意:如果列表“名称”中有很多条目,这将导致性能下降,因为这可能是NxN比较(在最坏的情况下)


Solution 2 : Use a set for the 'names' (Slightly better way) 解决方案2 :对“名称”使用一组(稍好一点的方法)

public boolean check(List<Person> people, Set<String> names) {
    for(Person person : people) {
        if (names.contains(p.getName()) {
            return true;
        }
    }
}


Solution 3 : Use a Map 解决方案3 :使用地图

Map<String, Person> personMap = new HashMap<>();

And then use 然后用

personMap.containsKey("John");

You could do roughly this if you your two types of object were made to have a common base class, and that base class implemented the "Comparable" interface. 如果您使两种类型的对象都具有一个公共基类,并且该基类实现了“可比较”接口,则可以大致执行此操作。

Then you can define different compareTo methods in each subclass to get the behaviour you want. 然后,您可以在每个子类中定义不同的compareTo方法以获得所需的行为。

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

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