Scott
Scott

Reputation: 9488

Case Insensitive filtering with 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));
}

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?

Upvotes: 0

Views: 880

Answers (2)

Jake iOS
Jake iOS

Reputation: 1

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]

Upvotes: 0

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

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]

Upvotes: 0

Related Questions