I have a collection of List<Foo>
elements Foo
:
class Foo {
private TypeEnum type;
private int amount;
}
Foo
It may be TypeEnum.A
and TypeEnum.B
.
I would like to get only those Foo
elements from the list that, if the element has type == TypeEnum.B
, then amount
more 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 Foo
with TypeEnum.B
, but without TypeEnum.A
.
source
share