Posts

Showing posts with the label Java Stream API

How to sum a List of integers using Java 8 Stream

In this section, we will show you how to  sum a List of integers using Java 8 Stream. Following ways can be used to sum a List of integers: 1. By using mapToInt() method  2. By using summarizingInt() method 3. By using reduce() method Example 1: By using mapToInt () method This mapToInt () method is an intermediate operation which returns an IntStream consisting of the results of applying the given function to the elements of this stream. import java.util.ArrayList ; import java.util.Arrays ; import java.util.List ; public class Main { // Driver code public static void main ( String [] args) { //Creating List of integers List < Integer > numberList = new ArrayList< Integer >( Arrays . asList ( 5 , 2 , 3 , 4 , 9 )); //Using mapToInt int sum = numberList .stream().mapToInt( Integer ::intValue).sum(); System . out .println( "Sum of integers => " + sum ); } } Console Output: Sum of integers =

How to transform HashMap to another HashMap using Java 8 Collectors toMap

Image
Hello everyone, here we will show you how to convert HashMap<Integer, User> to another HashMap<Integer, UserDto> using Java 8 Collectors toMap. The  Collectors.toMap returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements. Overview of components used in this example Java Stream API The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations(stream pipe-lining). The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. Collectors.toMap The

How to convert Set of objects to another Set of objects using Java streams?

Image
Hello everyone, here we will show you how to convert a Set to another Set in Java using Java streams map(). The ‘map’ method maps each element to its corresponding result.  Java Stream API The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations( stream pipe-lining ).  The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. Stream map() Method The  Java 8 Stream map()  is an intermediate operation.It converts Stream<obj1> to Stream<obj2>. For each object of type  obj1 , a new object of type  obj2  is created and put in the new Stream.

How to transform Array of objects to another Array of objects using Java 8 streams?

Image
Hello everyone, here we will show you how to convert an Array of objects to another Array of objects using Java streams map(). The ‘map’ method maps each element to its corresponding result.  Java Stream API The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations(stream pipe-lining). The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. Stream map() Method The Java 8 Stream map() is an intermediate operation.It converts Stream<obj1> to Stream<obj2>. For each object of type obj1, a new object of type obj2 is created and put in the ne

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Image
In this section, we will show you how to convert a List of objects to another List of objects in Java using the Java streams map(). The ‘map’ method maps each element to its corresponding result. Java Stream API   The Java Stream API provides a functional programming approach over the object oriented capabilities of Java. It is used for processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source List, Set or Array. Most of the stream operations return a Stream. This avails engender a chain of stream operations(stream pipe-lining). The streams withal support the aggregate or terminal operations on the elements. for example, finding the sum or maximum element or finding the average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream.   Stream map() Method   The Java 8 Stream map() is an intermediate operation. It converts Stream<obj1> to Stream<obj2>