Posts

Showing posts with the label Learn Java 8 streams with example

Java 8 Streams - Program To Break A List Into Batches Of Given Size

Image
In this post, we will show how to break a list into batches of a given size. Example 1: Convert List of Characters into batches of a given size import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class Main { public static void main( String [] args) { List < Character > list = Arrays .asList( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ); int chunkSize = 3 ; AtomicInteger ai = new AtomicInteger(); Collection < List < Character >> chunkedOrders = list.stream() .collect(Collectors.groupingBy(item -> ai .getAndIncrement() / chunkSize)) .values(); chun

Learn Java 8 Stream Intermediate And Terminal Operations with Example

Image
Java is Object Oriented Programming language, being object-oriented is not bad, but it brings a lot of verbosity to the program. Java 8 introduced new libraries and programming styles, for example, functional interfaces, lambda expressions, and streams. These bring functional-style programming to the object-oriented programming capabilities of Java. Java Functional Interface and Lambda Expression help us write smaller and cleaner code by removing much boilerplate code.  Java Stream Intermediate operations and Terminal operations The intermediate operation will transform a stream into another stream, such as a map(MapperFn) or a filter(Predicate). The Terminal operation will produce a result or side-effect, such as count() or forEach(Consumer). All intermediate operations will NOT be executed without a terminal operation at the end. So the pattern will be: stream() .intemediateOperation1() .intemediateOperation2() ....................... .intemediateOperationN() .ter

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.