Author: Editorial Team
-
Java 8 Collectors
In this tutorial we’ll discuss Collectors class and few of its important methods. The Stream.collect() method collect() method is provided by the Stream interface. This method performs a mutable reduction operation on the elements of this stream using a Collector. A mutable reduction operation accumulates input elements into a mutable result container, such as a…
-
map() vs flatMap() in Java
Overview Both map() and flatMap() are intermediate operations in Java’s Stream API. They are used to transform elements of a stream, but they differ in what they return and how they handle nested structures. 1. map() – Transform Each Element One-to-One Purpose: Method Signature: Characteristics: Example 1: Convert List of Strings to Uppercase Example 2:…
-
Stream flatMap() in Java with examples
Method Syntax Important points about flatMap() What is flattening? Let us understand flattening with the help of an example. Consider the following array: After flattening, the result is: So just to understand in simple terms, we can say that flattening is the merging of multiple collections into one. Need to flat a stream Sometime there…
-
Java 8 stream map() and example
This method is used when you want to process elements of a stream and apply some function on each element of a stream. We usually use map() while processing collection elements. An example to explain the use of map() is to write a program to multiply each element of a list of integers by 2.…
-
Java 8 filter() with examples
Java 8 Stream interface introduces filter() method which can be used to filter out elements based on a particular condition. The condition to filter elements is specified as a predicate. If the condition evaluates to true, the object is selected. Otherwise, it will be ignored. For example, you can use filter() to get even elements…
-
Convert Iterator to Stream in Java
Sometimes there is a need to convert Iterator to Stream to use lambda expression and other functional programming features. StreamSupport class provides stream(Spliterator spliterator, boolean parallel) method which returns Stream. The second argument in this method is for sequential or parallel stream. We need to provide true for parallel stream. Iterator does not have any…
-
Convert Iterable to Stream in Java
Iterables are very useful but provide limited support for lambda expressions. To get other features introduced in Java 8, sometimes there is a need to convert Iterable to Stream in Java. To convert Iterable to Stream, we first get a Spliterator reference. We then use StreamSupport class’ stream(Spliterator spliterator, boolean parallel) method to get Stream.…
-
Java Stream generate() method with example
Stream interface provides a method with following signature: This method returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. This method generates an infinite stream. We can add limit to get desired number of elements. Output
-
Difference between Streams and Collections in Java
A collection is a group of elements. A collection is an in-memory data structure. Collection supports various operations such as sorting, searching, insertion, manipulation and deletion. In Java, collection framework provides many interfaces like Set, List, Queue and implementation classes like ArrayList, HashSet to represent different type of collections. Stream is basically a sequence of…
-
How to create stream in Java 8
An instance of stream can be created from different sources. In this tutorial, we’ll discuss different ways to create a stream instance. First, we’ll discuss stream of four different sources: array, a collection, generator function and I/O channel. Stream of Array There are two common methods used to create stream of Array: Stream.of() and Arrays.stream().…
