Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<String> subset of another List<String> ignoring case?

Tags:

java

Imagine I have the following lists

List a - ("One", "Two", "Three", "Four", "Five")

List b - ("oNe", "two", "THREE")

I want to consider b as a subset of a (ignoring the case).

For now I'm using loops and a bit of lambda like so

boolean subset = true;                    
for(String bWord : b) {
   if(!a.stream().anyMatch(aWord -> aWord.equalsIgnoreCase(bWord))) {
     subset = false;
     break;          
   }
}

Is there a shorter way to do this maybe with lambdas?

like image 737
prettyvoid Avatar asked Dec 14 '22 16:12

prettyvoid


2 Answers

Convert arrays to lowercase:

a.stream().map(String::toLowerCase).collect(Collectors.toList());

and use containsAll:

b.containsAll(a);
like image 183
Maroun Avatar answered Dec 16 '22 07:12

Maroun


You can transform a to a HashSet of lower case Strings, which would make it faster to check for subset (since it would take constant time to check whether any element of b belongs to aset, instead of the linear time required for checking for inclusion in a List):

Set<String> aset = a.stream().map(String::toLowerCase).collect(Collectors.toCollection(HashSet::new));
boolean subset = b.stream().map(String::toLowerCase).allMatch(aset::contains);

P.S., you are using the term subset, but in fact what you are checking for is not whether one List is a subset of the other, since your Lists are not Sets, which means they may contain duplicate values. It would make more sense to begin with Sets in the first place.

EDIT:

If using containsAll seems better to you, at least run it on two Sets instead of two Lists. You'll get linear running time instead of quadratic running time:

Set<String> aset = a.stream().map(String::toLowerCase).collect(Collectors.toCollection(HashSet::new));
Set<String> bset = b.stream().map(String::toLowerCase).collect(Collectors.toCollection(HashSet::new));
boolean subset = aset.containsAll(bset);
like image 23
Eran Avatar answered Dec 16 '22 06:12

Eran