Posts

Showing posts with the label Java 8

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 convert a List of Lists to a List in Java 8

In this section, we will show you how to convert a List<List<Object>> to a List<Object> in Java 8 using Stream API. Following ways can be used for converting  List<List<Object>> to a List<Object> :  1. By using flatMap () method  2. By using forEach () method 3. By using mapMult () method Example 1: Use flatMap () method Java 8 Stream flatMap () method is used to flatten a Stream of collections to a stream of objects. The objects are combined from all the collections in the original Stream. Flattening is referred to as merging multiple collections/arrays into one. import java.util.Arrays ; import java.util.Collections ; import java.util.List ; import java.util.stream.Collectors ; public class Main { // Driver code public static void main ( String [] args) { //Creating List of Lists List < List < String >> listOfLists = Collections . singletonList ( Arrays . asList ( &quo

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

How to get keys and values from Map in Java?

In this section, we will show you h ow to get keys and values from Map in Java. Java 8 Example import java.util.HashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { Map < String , String > map = new HashMap<>(); map .put( "host" , "knowledgefactory.net" ); map .put( "username" , "knowledgefactory" ); map .put( "password" , "mypassword" ); // Java 8 map .forEach((k, v) -> { System . out .println( "Key: " + k + ", Value: " + v); }); } } Console Output: Key: password, Value: mypassword Key: host, Value: knowledgefactory.net Key: username, Value: knowledgefactory Old style import java.util.Collection ; import java.util.HashMap ; import java.util.Map ; import java.util.Set ; public class Main { public static void main ( String [] args) { Map < String , String >