Reputation:
Within Android, I'd like to perform an if statement to check whether an ArrayList contains any element from an array of Strings? e.g.
Check whether any of the elements from singingGroup are also containined in Winners[]
String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};
ArrayList<String> singingGroup = new ArrayList<String>();
singingGroup.add("Patrick");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Amy");
How can I do this? as I know how to check if one item is contained as in another array as below. But not if any from one, exist in another.
if (Arrays.asList(Winners).contains(singingGroup)) {
Upvotes: 3
Views: 6271
Reputation: 102
//for loop would be perfect to check if element i = element i
int i =0;
int loopCount = 0;
while(loopCount < Winners.lenght)
{
for(int i =0; i < singingGroup.length; i++)
{
if(Winners[loopCount] == singingGroup[i])
{
System.out.println(Winners[loopCount] + "is apart of the winners");
}//end of comparing if
if(i == singing.Group.length)
{
loopCount ++;
} //end of i == singingGroup
}//end of for loop
}//end of while loop
This is not the most optimal code but if you need it in a hurry this will work
Upvotes: 0
Reputation: 116
You can use the CollectionUtils class provided by Apache Commons.
Using the intersection method (useful if you want to do something with the common elements):
Collection<String> intersection = CollectionUtils.intersection(singingGroup, Arrays.asList(Winners));
if (intersection.size() > 0){
// At least one element contained in the intersection
}
Or, using the containsAny method:
if (CollectionUtils.containsAny(singingGroup, Arrays.asList(Winners))){
// True if at least one common element exists in both lists
}
Upvotes: 0
Reputation: 8606
Collections.disjoint is one way to archive this but You can also use retainAll() method.
Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};
ArrayList<String> singingGroup = new ArrayList<String>();
singingGroup.add("Patrick");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Amy");
List<String> WinnerList = new ArrayList<>(Arrays.asList(Winners));
WinnerList.retainAll(singingGroup);
System.out.println("retainList = " + WinnerList);
Output
list1 = []
String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};
ArrayList<String> singingGroup = new ArrayList<String>();
singingGroup.add("Steven");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Jennifer");
List<String> WinnerList = new ArrayList<>(Arrays.asList(Winners));
WinnerList.retainAll(singingGroup);
System.out.println("retainList = " + WinnerList);
Output
retainList = [Jennifer, Steven]
Upvotes: 2
Reputation: 367
you can also check like this:
String Winners[] = {"Jennifer", "Patrick", "Peter", "Parker"};
ArrayList<String> singingGroup = new ArrayList<String>();
singingGroup.add("Patrick");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Amy");
for(int i=0; i< Winners.length;i++)
{
if(singingGroup.contains(Winners[i]))
{
System.out.println("duplicate");
}
}
Upvotes: 0