[英]Find disjoint set irrespective of case of the strings
I am using Collection.disjoint to find the disjoint set of two string collections c1, c2.
我正在使用Collection.disjoint来查找两个字符串集合c1, c2.
的不相交集合c1, c2.
But it does not ignore cases, for example - string str
is different than Str
. 但它不会忽略大小写,例如 - string str
与Str
不同。
return Collections.disjoint(c1, c2);
Can I find the disjoint of both collections ignoring their cases without using a for loop? 如果不使用for循环,我能否找到忽略其案例的两个集合的不相交?
If you absolutely insist that no for
loop is used, you can always find the disjoint between two Collection
s of lowercased String
s. 如果你绝对坚持不使用for
循环,你总能找到两个Collection
of lowercased String
s之间的脱节。 Using Google Guava , it should be something like: 使用Google Guava ,应该是这样的:
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);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.