Posts

Showing posts with the label BigDecimal

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