Posts

Showing posts with the label Learn Java 8 streams with example

Java 8 Stream – How to sort a List with stream.sorted()

In this section, we will show you how to sort list using stream.sorted() in Java 8. 1. List 1.1 Sort a List in natural order. import java.util.Arrays ; import java.util.Comparator ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "8" , "B" , "A" , "1" , "Z" , "Y" , "2" , "b" , "d" ); List < String > sortedList1 = list .stream() .sorted( Comparator . naturalOrder ()) .collect( Collectors . toList ()); System . out .println( sortedList1 ); List < String > sortedList2 = list .stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect( Collectors . toList ()); System . out .println( sortedList2 ); List < String > sortedList3 = list .stream(). sorted().

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 =

Java 8 - forEach method that iterates with an index of an Array or List

In this section we will demonstrate how we can use the forEach() method with a specific index of an Array or List . 1. forEach() Method with an Array Index Generate the index with IntStream.range . import java.util.List ; import java.util.stream.Collectors ; import java.util.stream.IntStream ; public class Main { public static void main ( String [] args) { // Creating a list of string String [] list = { "Java" , "Kotlin" , "Go" , "Ruby" }; // Using forEach with index List < String > collect = IntStream . range ( 0 , list . length ) .mapToObj(index -> index + ":" + list [index]) .collect( Collectors . toList ()); collect .forEach( System . out ::println); } } Console Output: 0:Java 1:Kotlin 2:Go 3:Ruby 2. forEach() Method with a List and HashMap Index import java.util.Arrays ; import java.util.HashMap ; import java.util.List ; public class Main {

How to Sum BigDecimal using Java 8 Stream?

In this section, we will write a Java program to Sum BigDecimal using Java 8 Stream.  We can use the Stream.reduce() to sum a list of BigDecimal. 1. Stream.reduce() import java.math.BigDecimal ; import java.util.LinkedList ; import java.util.List ; public class Main { public static void main ( String [] args) { List < BigDecimal > salary = new LinkedList<>(); salary .add( BigDecimal . valueOf ( 2345.56 )); salary .add( BigDecimal . valueOf ( 3149.49 )); salary .add( BigDecimal . valueOf ( 3450.42 )); salary .add( BigDecimal . valueOf ( 4335.54 )); BigDecimal sum = salary .stream() .reduce( BigDecimal . ZERO , BigDecimal ::add); System . out .println( "Sum = " + sum ); } } Console Output: Sum = 13281.01 2. Map & Reduce Sum all  BigDecimal  from a list of  Users . package com.knf.dev.demo ; import java.math.BigDecimal ; import java.math.RoundingMode ; import java.util.Arrays ; impor

Java 8 Streams - Count Frequency Of Words In a List

Image
Example 1 import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Main { public static void main( String [] args) { List < String > words = Arrays .asList( "cat" , "rat" , "bat" , "cow" , "cat" , "bat" ); // For Long values Map < String , Long > result = words.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(result); } } The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. The asList () method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridg