I have a collection of List<Foo>elements Foo:
class Foo {
private TypeEnum type;
private int amount;
}
FooIt may be TypeEnum.Aand TypeEnum.B.
I would like to get only those Fooelements from the list that, if the element has type == TypeEnum.B, then amountmore than zero ( amount > 0).
How to do it using Java 8 Streams method filter()?
If I use:
List<Foo> l = list.stream()
.filter(i -> i.getType().equals(TypeEnum.B) && i.getAmount() > 0)
.collect(Collectors.<Foo>toList());
I get items Foowith TypeEnum.B, but without TypeEnum.A.
source
share