Lambdaj不区分大小写的过滤 [英] Case Insensitive filtering with lambdaj
问题描述
我正在尝试使自己熟悉lambdaj,但不确定如何解决此问题的最佳方法.进行以下测试:
I'm attempting to familiarize myself with lambdaj, and am unsure how the best way to solve this problem. Given the following test:
@Test
public void test() {
final List<String> l1 = new ArrayList<>();
final List<String> l2 = new ArrayList<>();
l1.add("same");
l1.add("Upper");
l1.add("lower");
l2.add("same");
l2.add("upper");
l2.add("lower");
l2.add("extra");
final List<String> l3 = Lambda.filter(Matchers.not(Matchers.isIn(l1)), l2);
Assert.assertThat("There should be one item in l3", l3.size(), Matchers.equalTo(1));
}
如何使Matcher不关心大小写?即我想要一个l2中不属于l1的项目列表,无论大小写如何?我宁愿不必运行另一个Lambda来将每个字符串列表转换为相同的大小写,而是一种修改Matcher以实现我希望的方式.这可能吗,还是我必须先将项目转换为相同的大小写?
How can I make the Matcher not care about case? i.e. I want a list of the items in l2 which are not in l1 irrespective of case? I'd prefer to not have to run another Lambda to convert each list of strings to the same case, but instead a way to modify the Matcher to do as I wish. Is this possible or do I have to convert the items to the same case first?
推荐答案
希望此答案可以为您提供帮助:
Hope this answer could help you:
List<String> convert = convert(list1, new Converter<String, String>() {
@Override
public String convert(String from) {
return from.toLowerCase();
}
});
List<String> filter2 = filter(isIn(list2), convert);
System.out.println("filter2 -> " + filter2);
// filter2 -> [same, upper, lower]
这篇关于Lambdaj不区分大小写的过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!