Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Neo Stream Search Java Challenge

  • July 26, 2021
  • 1463 Unique Views
  • 2 min read
Table of Contents
Neo Stream Search Challenge

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/

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (3)

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.

AR avatar

AR

3 years ago

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

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.

David avatar

David

3 years ago

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.

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.

Rafael del Nero avatar

Rafael del Nero

3 years ago

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.

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.

Subscribe to foojay updates:

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