java : reduce stream of list of maps


package homeWork;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class sumSample {


public static void main(String[] args) {

List> countries = new ArrayList>();
Map country;
country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", new BigDecimal("127156000"));
countries.add( country );

country = new HashMap();
country.put( "name", "france" );
country.put( "currency", "EUR" );
country.put( "population", new BigDecimal("65073482"));
countries.add( country );

country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", new BigDecimal("56000"));
countries.add( country );

country = new HashMap();
country.put( "name", "spain" );
country.put( "currency", "EUR" );
country.put( "population", new BigDecimal("44904000"));
countries.add( country );

country = new HashMap();
country.put( "name", "russia" );
country.put( "currency", "RUB" );
country.put( "population", new BigDecimal("141903979") );
countries.add( country );

for(Mapct:countries) {
System.out.println(ct.get("name")+":"+ct.get("population"));
}
System.out.println("-----------------------");

BigDecimal totalBg=BigDecimal.ZERO;
totalBg = countries.stream().map(m -> ((BigDecimal) m.get("population"))).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("sum: "+totalBg.toString());
}
}

result:

japan:127156000
france:65073482
japan:56000
spain:44904000
russia:141903979

                                            • -

sum: 379093461