See this code: (This is working code but I don't like to use 2 lines so looking for how can I make it better)
ItemDetails[] items = response.getDetailsList();
items = Arrays.stream(items).filter(x -> !x.getName().equalsIgnoreCase("acl1")).toArray(ItemDetails[]::new);
items = Arrays.stream(items).filter(x -> !x.getName().equalsIgnoreCase("acl2")).toArray(ItemDetails[]::new);
I could not figure how to use an OR (|)
condition within filter
to remove two specific elements from List
since it would give me compile time error (in IDE) and I end up using two filters
like above.
What could I be missing?
This is how I tried to use OR
items = Arrays.stream(items).filter(x -> !x.getName().equalsIgnoreCase("acl1") ||
x -> !x.getName().equalsIgnoreCase("acl2")).toArray(ItemDetails[]::new);
=> IntelliJ would complain about this one (above)
items = Arrays.stream(items).filter(x -> !x.getName().equalsIgnoreCase("acl1"") ||
x.getName().equalsIgnoreCase("acl2")).toArray(ItemDetails[]::new);
Does not work at runtime (it does not filter)
Entire code with commented code (which I tried) for reference
public static void mapTest () {
AclDetailItem[] items = new AclDetailItem[3];
AclDetailItem item1 = new AclDetailItem();
item1.setAclName("acl1");
AclDetailItem item2 = new AclDetailItem();
item2.setAclName("acl2");
AclDetailItem item3 = new AclDetailItem();
item3.setAclName("acl3");
items[0] = item1;
items[1] = item2;
items[2] = item3;
System.out.println ("Before " + items.length);
items = Arrays.stream(items).filter(x -> !x.getAclName().equalsIgnoreCase("acl1")).toArray(ItemDetails[]::new);
items = Arrays.stream(items).filter(x -> !x.getAclName().equalsIgnoreCase("acl2")).toArray(ItemDetails[]::new);
System.out.println ("After " + items.length);
}
You should create multiple predicates for each condition and join it using or. I have added my below example:
public class MeTest {
Predicate<Integer> con1 = i -> i==0;
Predicate<Integer> con2 = i -> i==1;
Predicate<Integer> con3 = i -> i==2;
@Test
public void testOr() {
List<Integer> asdas = IntStream.range(1, 10).boxed().filter(con1.or(con2).or(con3)).collect(toList());
System.out.println("asdas = " + asdas);
}
}
items = Arrays.stream(items).filter(
x -> !x.getName().equalsIgnoreCase("redirect")
|| x->!x.getName().equalsIgnoreCase("success"))
.toArray(ItemDetails[]::new);
Note the second x->
. That's used to bind a variable at the start of a lambda. Putting it in the middle is invalid, and unnecessary since x
already exists in the scope. Just remove that bit:
items = Arrays.stream(items).filter(x ->
!x.getName().equalsIgnoreCase("redirect")
|| !x.getName().equalsIgnoreCase("success"))
.toArray(ItemDetails[]::new);
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