Giter Club home page Giter Club logo

Comments (1)

Maiklins avatar Maiklins commented on July 27, 2024

Hi @moun3imy!
sorted() prevents lazy evaluation of all intermediary operation before it's called, but not after.
Let's consider the following example:

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntUnaryOperator;
import java.util.stream.IntStream;

public class SortedStreamTest {

    public static void main(String[] argv) {

        final AtomicInteger mapCount = new AtomicInteger(0);
        IntUnaryOperator plus1 = i -> { mapCount.getAndIncrement(); return i + 1; };
        IntStream stream = IntStream.of(3, 5, 7, 10, 2, 6, 8);

        int firstElement = stream
                .map(plus1)
                //.sorted()
                //.map(plus1)
                .findFirst()
                .getAsInt();

        System.out.println(firstElement);
        System.out.println("Map count: " + mapCount.get());
    }
}

In the above example, the mapping operation plus1 is executed only once, so here we have lazy evaluation.
However, if we call sorted() after the mapping, the mapping is executed 7 times:

int firstElement = stream 
.map(plus1) 
.sorted() 
//.map(plus1) 
.findFirst() 
.getAsInt();

Whereas if we call the mapping after sorted(), it's evaluated only once:

int firstElement = stream 
//.map(plus1) 
.sorted() 
.map(plus1) 
.findFirst() 
.getAsInt();

The applied sorting algorithm depends on the type of stream:
no sorting is necessary for already sorted streams, e.g. SortedSet
for non-parallel streams, Arrays.sort() is used
for non-parallel streams, Arrays.parallelSort() is used
The sort is executed completely.

from stackify.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.