Java Streams .skip() and .limit() errors

Ultimate Java Part 3: Advance Topics → Streams → Sorting Streams video.

movies.stream().skip(10).limit(15).forEach(movie -> System.out.println(movie));
movies.stream().limit(15).skip(5).forEach(movie -> System.out.println(movie));

Greetings. When I combine the skip() and limit() methods as shown, to paginate the List of Movies, it results as shown below. Note: I reversed the position of the methods to test conditions, and it makes no difference.

src.Streams.Movie@129a8472
src.Streams.Movie@1b0375b3
src.Streams.Movie@2f7c7260

This only happens when I combine the methods. So, in every other case, I get the correct outcome.

I use these long-term support SDK and Language levels

image

My List of movies is 30 elements long, and numbered for proper testing. See the SUMMARY below.

Summary
List<Movie> movies = List.of(
            new Movie("1: Remember The Titans", 10),
            new Movie("2: Any Given Sunday", 15),
            new Movie("3: Fireproof", 20),
            new Movie("4: Faith Like a Potato", 25),
            new Movie("5: Mall Cop", 30),
            new Movie("6: Desperado", 35),
            new Movie("7: Avengers: Age of Ultron", 40),
            new Movie("8: Once Upon A Time In Mexico", 45),
            new Movie("9: Once Upon A Time In Hollywood", 20),
            new Movie("10: The GodFather", 55),
            new Movie("11: Tarzan", 60),
            new Movie("12: Black Widow", 65),
            new Movie("13: Iron Man", 70),
            new Movie("14: Spider Man", 75),
            new Movie("15: The Jetsons", 80),
            new Movie("16: Crouching Tiger, Hidden Dragon", 15),
            new Movie("17: Dakra", 90),
            new Movie("18: Hero", 95),
            new Movie("19: Dark Phoenix", 100),
            new Movie("20: Darkra", 105),
            new Movie("21: Gladiator", 110),
            new Movie("22: The Crow", 115),
            new Movie("23: The Pale Blue Pearl", 120),
            new Movie("24: The Note Book", 125),
            new Movie("25: Silver linings' Note Book", 130),
            new Movie("26: The Man of Steel", 135),
            new Movie("27: The Pianist", 140),
            new Movie("28: Schindler's List", 145),
            new Movie("29: Spawn", 150),
            new Movie("30: Knights Of The Zodiac", 155));

This use of skip() does not produce the error.

movies.stream().skip(20).forEach( m -> System.out.println(m.getTitle()));

This use of limit() does not produce an error either.

movies.stream().limit(10).forEach( m -> System.out.println(m.getTitle()));

Where am I going wrong?

I have been coding for many hours non-stop. It’s a habit. Y’all need to forgive me on this probably silly one. Thanks in advance.

Here are the lines of code that produce the error, see SUMMARY below:

Summary
movies.stream().skip(2).limit(30).forEach(movie -> System.out.println(movie));
        System.out.println();
        System.out.println("Skip 0 and limit 30 , limit 10 skip 5 TEST\n");

    //  Skip 0 and limit 30 , limit 10 skip 5 TEST
        movies.stream().skip(10).limit(15).forEach(movie -> System.out.println(movie));
        movies.stream().limit(15).skip(5).forEach(movie -> System.out.println(movie));

Here is the full result of those lines of code, see SUMMARY below:

Summary
This is the algorithm to paginate data, see SUMMARY below:

src.Streams.Movie@129a8472
src.Streams.Movie@1b0375b3
src.Streams.Movie@2f7c7260
src.Streams.Movie@2d209079
src.Streams.Movie@6bdf28bb
src.Streams.Movie@6b71769e
src.Streams.Movie@2752f6e2
src.Streams.Movie@e580929
src.Streams.Movie@1cd072a9
src.Streams.Movie@7c75222b
src.Streams.Movie@4c203ea1
src.Streams.Movie@27f674d
src.Streams.Movie@1d251891
src.Streams.Movie@48140564
src.Streams.Movie@58ceff1
src.Streams.Movie@7c30a502
src.Streams.Movie@49e4cb85
src.Streams.Movie@2133c8f8
src.Streams.Movie@43a25848
src.Streams.Movie@3ac3fd8b
src.Streams.Movie@5594a1b5
src.Streams.Movie@6a5fc7f7
src.Streams.Movie@3b6eb2ec
src.Streams.Movie@1e643faf
src.Streams.Movie@6e8dacdf
src.Streams.Movie@7a79be86
src.Streams.Movie@34ce8af7
src.Streams.Movie@b684286

Skip 0 and limit 30 , limit 10 skip 5 TEST

src.Streams.Movie@1cd072a9
src.Streams.Movie@7c75222b
src.Streams.Movie@4c203ea1
src.Streams.Movie@27f674d
src.Streams.Movie@1d251891
src.Streams.Movie@48140564
src.Streams.Movie@58ceff1
src.Streams.Movie@7c30a502
src.Streams.Movie@49e4cb85
src.Streams.Movie@2133c8f8
src.Streams.Movie@43a25848
src.Streams.Movie@3ac3fd8b
src.Streams.Movie@5594a1b5
src.Streams.Movie@6a5fc7f7
src.Streams.Movie@3b6eb2ec
src.Streams.Movie@2d209079
src.Streams.Movie@6bdf28bb
src.Streams.Movie@6b71769e
src.Streams.Movie@2752f6e2
src.Streams.Movie@e580929
src.Streams.Movie@1cd072a9
src.Streams.Movie@7c75222b
src.Streams.Movie@4c203ea1
src.Streams.Movie@27f674d
src.Streams.Movie@1d251891


Process finished with exit code 0

FOR REFERENCE ONLY, THIS IS HOW MOSH DOES IT:

OMG :neutral_face:

Nevermind. I found the issue. After almost two hours. Here was the problem.
:sweat_smile:

Summary
movies.stream().skip(10).limit(15).forEach(movie -> System.out.println(movie));
movies.stream().limit(15).skip(5).forEach(movie -> System.out.println(movie));

Here is the solution:

Time to sleep, it’s 5:20 am, been practicing since 4 pm yesterday. :melting_face: