1

How do I convert this below if/else statement to java stream pipeline?

I have an Optional<> and if That is present then I am making a database call which returns a list and I want to stream over that list and if certain condition is met I want to perform an operation and if not I want to perform some other operation

if(returnOptional().isPresent) {
    List<Object> list = db.findAllById(id);
    if(list.stream().anyMatch(x -> /*predicate*/)){
        //perform function
    } else {
        //perform function
    }
}

Partial Solution :

      returnOptional()
      .map(obj -> {
        List<Object> list = db.call();
        return list.stream()
          .filter(/*Predicate*/)
          .findAny().map(object -> /*function*/)
          .orElse(null);
    }).orElseGet(() -> /*function*/);

java user
  • 67
  • 1
  • 9
  • You should clarify, what kind of function you want to execute here. Is it some operation on the found x, or something to be performed on an otherwise not related object? – juwil Jun 10 '21 at 11:53
  • So you kind of want to branch the stream into two and perform different logic on each of the two branches? – Philipp Jun 10 '21 at 11:54
  • @Philipp yes, you got that right – java user Jun 10 '21 at 11:55
  • 2
    Instead of `anyMatch(predicate)` you could try `filter(predicate).findAny().isPresentOrElse(/*consumer for true*/, /*runnable for false*/)`. – Thomas Jun 10 '21 at 11:59
  • @Philipp that's something different – java user Jun 10 '21 at 12:03
  • @Thomas Thanks thomas but this function is not in java8 which is the one I am using – java user Jun 10 '21 at 12:04
  • How is it different? – Philipp Jun 10 '21 at 12:04
  • Well `.map(/*...*/).orElseGet(/*...*/)` should work as well – Thomas Jun 10 '21 at 12:06
  • @Philipp I have an ```Optional<>``` and if That is present then I am making a database call which returns a list and I want to stream over that list and if certain condition is met I want to perform an operation and if not I want to perform some other operation – java user Jun 10 '21 at 12:09
  • And which part of that would not be possible with the answers to the question I linked to? – Philipp Jun 10 '21 at 12:11
  • @Philipp Now I got you phillip. Thanks – java user Jun 10 '21 at 12:45
  • As of Java 12 you can use `Collectors.teeing()` to create two streams and process each one separately. – WJS Jun 10 '21 at 13:09
  • 1
    Why is this operation dependent on the presence of `returnOptional()` when it never uses its value? Besides that, there’s no sense in converting the second if/else statement to java stream pipeline. It’s a single condition and the canonical syntax for doing something when the condition is fulfilled and something else when not is an if/else statement. – Holger Jun 10 '21 at 13:57

2 Answers2

1

You can do something like this:

if(returnOptional().isPresent) {
List<Object> list = db.findAllById(id);

list.stream().map(object -> {
    if(/*predicate logic*/) {
        // perform function if predicate logic true
    }
    else {
        // perform function if predicate logic false
    }
    return object;
  });
}

but as philip mentioned technically you cannot split a stream into two streams and collect.

0
        Optional.ofNullable(list).ifPresent( y -> {
                    if (y.stream().anyMatch(x -> /*predicate*/)) {
                        System.out.println("a");
                    } else {
                        System.out.println("b");
                    }
                });
AmonTin
  • 1
  • 1