I am using Collection.disjoint to find the disjoint set of two string collections c1, c2. But it does not ignore cases, for example - string str is different than Str.
return Collections.disjoint(c1, c2);
Can I find the disjoint of both collections ignoring their cases without using a for loop?
If you absolutely insist that no for loop is used, you can always find the disjoint between two Collections of lowercased Strings. Using Google Guava, it should be something like:
package ru.zombator.stackoverflow;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
public final class DisjointIgnoreCase {
public static void main(String[] args) {
Collection<String> coll1 = Arrays.asList("donald", "Duck");
Collection<String> coll2 = Arrays.asList("DONALd", "Donut");
Collection<String> coll3 = Arrays.asList("Homer", "DONUT");
Collection<String> coll4 = Arrays.asList("DONALD", "duck");
// will all print false
System.out.println(disjointIgnoreCase(coll1, coll2));
System.out.println(disjointIgnoreCase(coll2, coll3));
System.out.println(disjointIgnoreCase(coll1, coll4));
// will print true (no common elements)
System.out.println(disjointIgnoreCase(coll1, coll3));
}
private static boolean disjointIgnoreCase(Collection<String> coll1, Collection<String> coll2) {
return Collections.disjoint(lowercased(coll1), lowercased(coll2));
}
private static Collection<String> lowercased(Collection<String> coll) {
return Collections2.transform(coll, new Function<String, String>() {
@Override
public String apply(String input) {
return input.toLowerCase(Locale.US);
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With