user3325783
user3325783

Reputation:

Check if two different ArrayLists are Equal

I have an Array List of type String and an Array List of type Person. Where Person is an Object that only contains a String that holds the name.

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.

Is there a way to check if the names Array List contains the Person who is named 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?

Upvotes: 1

Views: 2814

Answers (5)

Anderson Vieira
Anderson Vieira

Reputation: 9049

In Java 8 you can do this using Streams.anyMatch():

Return true if there is any Person in people whose name is in 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>.

Upvotes: 2

redge
redge

Reputation: 1192

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;

Upvotes: 0

MJSG
MJSG

Reputation: 1025

Possible Solutions:

Solution 1: Simplest approach

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)


Solution 2: Use a set for the 'names' (Slightly better way)

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

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

And then use

personMap.containsKey("John");

Upvotes: 0

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

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 (Person p : people) {
    if (names.contains(p.getName()) {
       return true;
    }
 }

Upvotes: 4

Jelly legs mick
Jelly legs mick

Reputation: 33

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.

Upvotes: -1

Related Questions