Case Insensitive filtering with lambdaj

﹥>﹥吖頭↗ 提交于 2019-12-12 03:03:25

问题


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));
}

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?


回答1:


This works for me:

import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
//..
List<String> allJedis = asList("Luke","Obiwan","Yoda");
List<String> someJedis = filter(equalToIgnoringCase("obiwan"), allJedis);
System.out.println(someJedis);

Output is [Obiwan]




回答2:


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]


来源:https://stackoverflow.com/questions/11022266/case-insensitive-filtering-with-lambdaj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!