Friends of OpenJDK Today

Neo Stream Search Java Challenge

July 26, 2021

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Understanding the mechanics of the functional interface Predicate of a Stream is crucial if you want to create something meaningful with streams. On this challenge, we will explore important key methods when we work with a stream so that it becomes clear for you what they do.

Without further ado, let’s go to the Java Challenge!

It’s time to improve your Java skills with this Neo Stream Search Challenge

Neo Stream Search Challenge

What do you think will happen when running the following code?

import java.util.List;
import java.util.function.Predicate;

public class NeoSearch {

    public static void main(String... doYourBest) {
        List<String> ls = List.of("Neo", "Morpheus", "Oracle", "Trinity", "Neo");

        Predicate<String> neoSearch = str -> {
            System.out.println("Agent Smith is looking for Neo...");
            return str.contains("Neo");
        };

        var binaryNumbers = List.of(1, 0, 1, 1).stream();
        Integer binarySum = binaryNumbers.reduce(Integer::sum).orElseThrow(StackOverflowError::new);

        boolean neoFound = ls.stream().filter(str -> str.length() >= binarySum).allMatch(neoSearch);
        System.out.println(neoFound);
    }

}

A) Agent Smith is looking for Neo…​
false

B) Agent Smith is looking for Neo…​
Agent Smith is looking for Neo…​
false

C) Agent Smith is looking for Neo…​
true

D) Agent Smith is looking for Neo…​
Agent Smith is looking for Neo…​
Agent Smith is looking for Neo…​
Agent Smith is looking for Neo…​
Agent Smith is looking for Neo…​
true

Explanation:

Here we are simply defining what is the Predicate condition with a Lambda:

Predicate neoSearch = str -> {
  System.out.println("Agent Smith is looking for Neo...");
  return str.contains("Neo");
};

Then we are reducing our List from 1, 0, 1 and 1 to 3 because we are adding all elements basically:

var binaryNumbers = List.of(1, 0, 1, 1).stream();
Integer binarySum = binaryNumbers.reduce(Integer::sum)
.orElseThrow(StackOverflowError::new);

Then here we filter all elements that have the size greater or equals to binarySum and then we use the allMatch function. Note that this function has to have all elements matching to return true. Since the first element is true, the allMatch method from the Stream will go to the next element printing again "Agent Smith is looking for Neo…​", as this method requires all elements to be true, there is no point in continuing the looping so the looping will break.

boolean neoFound = ls.stream().filter(str -> str.length() >= binarySum).allMatch(neoSearch);
System.out.println(neoFound);

To conclude, the output will be:

B) Agent Smith is looking for Neo...
    Agent Smith is looking for Neo...
    false

To fully understand this Java Challenge, you can also watch the FULL explanation video:

That’s it challenger! Keep it up with your Java learning and keep solving the Java Challenges! To see the original article, check it out the following link:
https://javachallengers.com/neo-stream-search-java-challenge/

Topics:

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Comments (3)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

AR

The stream does an optimization and drops when it hits false the first time

David

Hello Rafael
For information, the list in the blog post (List.of(“Neo”, “Neo”, “Morpheus”, “Neo”, “Neo”)) is not the same as the one in the video (List.of(“Neo”, “Morpheus”, “Oracle”, “Trinity”, “Neo”)). So the output is not the same too.
Regards.

Rafael del Nero

Hello David, thanks for your comment. I just fixed this Java Challenge to the right values in the List.of method. It should be fine now! I also added the written explanation.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard