Your Brand Voice sounds different?

Customer Relationship is often misinterpreted by managers, leading towards misconduct of relationship, and eventually losing them. The traditional form of Customer Relationship management often fails…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Leveraging the Laziness of Streams

Functional Programming in Java — by Venkat Subramaniam (54 / 96)

👈 Lazy Evaluations | TOC | Creating Infinite, Lazy Collections 👉

In previous chapters we’ve seen the facilities that the new Stream interface offers, but so far we haven’t discussed one of their most salient features — they’re really lazy, in a good way. The lazy evaluation of Streams is quite powerful. First, we don’t have to do anything special to derive their benefits. In fact, we’ve used them many times already! Second, they can postpone not just one, but a sequence of evaluations so that only the most essential parts of the logic are evaluated, and only when needed. Let’s look at how lazy Streams are and how we benefit from that.

Streams have two types of methods: intermediate and terminal, which work together. The secret behind their laziness is that we chain multiple intermediate operations followed by a terminal operation.

Methods like map and filter are intermediate; calls to them return immediately and the lambda expressions provided to them are not evaluated right away. The core behavior of these methods is cached for later execution and no real work is done when they’re called. The cached behavior is run when one of the terminal operations, like findFirst and reduce, is called. Not all the cached code is executed, however, and the computation will complete as soon as the desired result is found. Let’s look at an example to understand this better.

Suppose we’re given a collection of names and are asked to print in all caps the first name that is only three letters long. We can use Stream’s functional-style methods to achieve this. But first let’s create a few helper methods.

The two helper methods simply print the parameters they receive before returning the expected results. We wrote these methods to take a peek at the intermediate operations in the code we’ll…

Add a comment

Related posts:

15.3 Review of MOP Techniques

Groovy brings you the best of both worlds: a flexible, highly productive, agile, dynamic language that runs on the rich framework of the Java Platform. Groovy preserves the Java semantics and extends the JDK to give you true dynamic language capabilities. Programming Groovy 2 will help you, the experienced Java developer, lea

BADSHAHI MOSQUE

Badshahi mosque is one of the few significant architectural monuments built during Emperor Aurangzeb’s long rule from 1658 to 1707. It is presently the fifth largest mosque in the world and was…

Bank Term Deposit Prediction

Banks run a campaign to get its clients make a term deposit. A term deposit is a fixed-term investment that includes the deposit of money into an account at a financial institution. Term deposit…