-1

So currently I am trying to wrap my head around java 8.

I have this stream manipulation

    List<MyObject> parsedEvents = events.stream()
.filter(Objects::nonNull)
    .map(
         e -> MyObject.builder()
.setFoo(e.foo())
.setBar(e.bar())
.setBaz(e.baz())
.build()
    ).collect(Collectors.toList());

But, sometimes e.foo() can be Null or e.bar() can be null or e.baz() can be null.

So, I want to just filter those events where any of those methods will return a null.

What's a clean way to do that in java 8.

user207421
  • 305,947
  • 44
  • 307
  • 483
frazman
  • 32,081
  • 75
  • 184
  • 269
  • add a condition to your existing filter that is desired as such, or are you looking for anything else? pertaining to the title, its purely a duplicate. – Naman Sep 28 '20 at 01:30
  • There is no such thing as a null object, only null references. – user207421 Sep 28 '20 at 01:37

1 Answers1

1

Try this.

List<MyObject> parsedEvents = events.stream()
    .filter(Objects::nonNull)
    .filter(e -> Stream.of(e.foo(), e.bar(), e.baz())
        .allMatch(Objects::nonNull))
    .map(
        e -> MyObject.builder()
            .setFoo(e.foo())
            .setBar(e.bar())
            .setBaz(e.baz())
            .build())
    .collect(Collectors.toList());