Last week, I described a use-case for a custom Stream Collector
. I received a intriguing comment on Twitter:
Hats off to you, Miguel! Your comment revealed a method I didn't know!
Tips, Expertise, Articles and Advice from the Pro's for Your Website or Blog to Succeed
Last week, I described a use-case for a custom Stream Collector
. I received a intriguing comment on Twitter:
Hats off to you, Miguel! Your comment revealed a method I didn't know!
Java Stream's Collectors
methods fit most use-cases. They allow returning either a Collection
or a scalar. For the former, you use one of the toXXX()
method, for the latter, one of the reducing()
one.
Let's imagine an e-commerce platform that implements a shopping cart. The cart is modeled as the following:
It’s been four years since functional programming became feasible in Java. The means we have had four years to play with Java 8.
And we've played... and played. After developing several large enterprise projects that made heavy use of Lambdas and Streams, consulted many other projects in doing so, and taught these concepts to hundreds of developers as an independent trainer, I think it’s time to summarize patterns, best practices, and anti-patterns.
“Reactive Streams”, whenever we come across these words, there are two things that come to our mind. The first is asynchronous stream processing and the second is non-blocking backpressure. In this blog, we are going to learn about the latter part.
According to the English dictionary, back pressure means “Resistance or force opposing the desired flow of fluid through pipes”. To define it in our software context, in place of the flow of fluid we can say the flow of data “Resistance or force opposing the desired flow of data through software”
Despite Java not providing a zip operation, you don't need either 30 additional lines to implement it, nor a third party library. Simply compose a zipline through the existing Stream API.
Java, from its 8th version onward, provides an easy way to query sequences of elements through its Stream Interface, which provides several operations out of the box. This set of operations is quite versatile but, as can be expected, it does not cover all the operations a programmer may require. One such operation is zip, as we can observe in one of the most visited posts about Java Streams in Stackoverfow: Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip). Even 7 years later, we are now on Java 14 release and there is no zip operation for Streams yet.
Did you know that you can join database tables into a Java Stream with Java 14's preview record
feature? Read this short article and find out how it is done using the Speedment Stream ORM. We will start with how to set up your project.
You may also like: A First Look at Records in Java 14
Download Java 14. Go to the Speedment Initializer and download your project skeleton (including pom.xml
). Modify the following lines in your pom.xml
file.
Occasionally, we will run into a situation in which we want to modify a variable inside a Lambda expression, but when we try to do so, we get a compile-time error saying:
A variable inside Lambda must be Final or Effective Final.
In this article, I would like to share few scenarios where we will see how the Java 8 Stream and related functions can be useful for reading a file and applying some aggregate functions, which we generally do in our day-to-day SQL/queries on the database side.
So, I am taking an example of employee CSV files, which is shown below:
It’s been more than a year since JDK 9 was released, and if you haven't touched Java 9, this year is probably the best time to learn Java 9 and get yourself familiar with new features and enhancements on existing APIs. The highlight of JDK 9 release was Project Jigsaw or Module System.
The Java 9 Platform Module System allows Java to move forward by modularizing the JDK as well as adding modules as first-class citizens to Java, but that’s not the only thing to look forward, there are many small changes that are very useful in day-to-day coding.
Recently, while developing examples for the groupingBy() guide, I discovered the handy JDK 10 additions to the Stream API Collectors, allowing collecting Stream pipelines into unmodifiable data structures.
In this super short article, we'll have a closer look at them.