Posts

Showing posts with the label Java Stream API

10 Common Java Interview Questions Related to the Stream API

Java interview questions related to streams are often focused on practical usage of the Stream API introduced in Java 8. Some of the most common practical questions include: 1. Filtering a List Problem: Given a list of integers, filter out the even numbers and return a list of odd numbers. Solution: import java.util.Arrays ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < Integer > numbers = Arrays . asList ( 1 , 2 , 3 , 4 , 5 , 6 ); List < Integer > oddNumbers = numbers .stream() .filter(n -> n % 2 != 0 ) .collect( Collectors . toList ()); } } 2. Mapping a List Problem:  Given a list of strings, convert all strings to uppercase. Solution: import java.util.Arrays ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > words =...

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. Collect...

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 ...