Friends of OpenJDK Today

Stream Limit Filter Java Challenge

August 17, 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

To manipulate data from a collection by using a Java stream is handy and cleaner than the alternatives.

When you learn how to use streams, your Java code will be much better, and knowing how to limit and filter data with streams is crucial for you to do something useful with streams in Java.

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

It's time to improve your Java skills with this Limit Stream Challenge.

Limit Stream Java Challenge

What will happen after the main method below is executed?

import java.util.stream.IntStream;

public class LimitStreamChallenger {

    public static void main(String... doYourBest) {
        IntStream.iterate(0, i -> i + 1)
                .limit(5)
                .filter(i -> i % 2 == 0)
                .forEach(System.out::print);

        IntStream.iterate(0, i -> i + 1)
                .peek(System.out::print)
                .filter(i -> i % 2 == 0)
                .limit(3)
                .forEach(System.out::print);
    }

}

A) 024002244
B) 02400122344
C) 02401234
D) 02401234

Explanation:

Let's analyze the stream code:

IntStream.iterate(0, i -> i + 1).limit(5).filter(i -> i % 2 == 0)                 
.forEach(System.out::print); 

Note that we are using an infinite stream at the iterate line, then we limit our stream to 5 elements and filter it by even numbers and finally print all of them. Since we limit the numbers to 5, we will have our stream iterating in a range from 0 to 4 on our stream. Then we filter it by even numbers and we finally print the elements and the first result will be 024.

In the second stream, we use a very similar structure to the first stream, but the difference is the "peek" method and the order of the filter and limit methods. The peek method will print all the elements on the conditions of this stream. Remember that the peek method is meant to debug and help us understand what is happening in the steam. Then we will filter the stream elements to the even numbers, we will have here 0, 2 and 4 since we are limiting by 3.

Note that in this stream, we do have 3 elements, which is because the limit is being performed on top of the filter method.

As the peek method will print all elements, the filtered and non-filtered, the final output will be... what do you think?

That’s it challenger, keep breaking your limits and stay consistent with your goals!

Topics:

Related Articles

View All
  • Function Calculation Java Challenge

    Functional programming is a very powerful paradigm that makes code more concise and easier to understand.

    In Java, the “Function” functional interface can be used as a first-class citizen function, meaning that we can pass a function to a method and declare it as a variable, giving the developer a lot of power.

    And now it’s time for you now to test your abilities with lambdas and functional interfaces!

    Read More
    September 01, 2021
  • Jedi Lambda Join Java Challenge

    There are many concepts involved in Java Challenges! In essence, we will continue exploring lambdas and the Function interface the most.

    However, we also have static methods introduced in Java 8 and the private method in interfaces introduced in Java 9. In the invocation of the interface methods, we are using anonymous inner classes too!

    Are you ready for the next Java Challenge? Less introduction, more action, try out this Java Challenge, and master Java concepts by having fun!

    Read More
    July 21, 2021
  • Type Erasure Generics Java Challenge

    It’s possible to use type erasure generics in a method with Java. To know how to use generics is important because then you are able to create highly reusable code.

    In the following Java Challenge, you will see the generic type that will be erased by the compiler and then will be replaced by the type we defined at runtime.

    Note also that we are using the extends keyword which means that the generic type will extend the other type.

    Without further ado, it’s time to solve the Java Challenge quiz!

    Read More
    May 25, 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

Comments (0)

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.

Subscribe to foojay updates:

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