Java tutorial
//package com.java2s; /* * This software is in the public domain under CC0 1.0 Universal plus a * Grant of Patent License. * * To the extent possible under law, the author(s) have dedicated all * copyright and related and neighboring rights to this software to the * public domain worldwide. This software is distributed without any * warranty. * * You should have received a copy of the CC0 Public Domain Dedication * along with this software (see the LICENSE.md file). If not, see * <http://creativecommons.org/publicdomain/zero/1.0/>. */ import java.math.BigDecimal; import java.util.*; public class Main { /** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal; * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */ public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList, String fieldName, BigDecimal stdDevMultiplier) { BigDecimal total = BigDecimal.ZERO; BigDecimal squaredTotal = BigDecimal.ZERO; int count = 0; for (Map<String, Object> dataMap : dataList) { if (dataMap == null) continue; BigDecimal value = (BigDecimal) dataMap.get(fieldName); if (value == null) continue; total = total.add(value); squaredTotal = squaredTotal.add(value.multiply(value)); count++; } if (count < 2) return null; BigDecimal countBd = new BigDecimal(count); BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP); double totalDouble = total.doubleValue(); BigDecimal stdDev = new BigDecimal(Math .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1))); Map<String, BigDecimal> retMap = new HashMap<>(6); retMap.put("total", total); retMap.put("squaredTotal", squaredTotal); retMap.put("count", countBd); retMap.put("average", average); retMap.put("stdDev", stdDev); if (stdDevMultiplier != null) retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier))); return retMap; } }