使用Lambda查找平均值(Double存储为String) [英] Finding average using Lambda (Double stored as String)

查看:519
本文介绍了使用Lambda查找平均值(Double存储为String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的对象

Map<String, List<ObjectDTO>> mapOfIndicators = new HashMap<>();

ObjectDTO

ObjectDTO is

public class ObjectDTO {
 private static final long   serialVersionUID = 1L;

    private String iName;
    private String dName;
    private String countryName;
    private int year;

    //Since the value can be empty, using String instead of Double
    private String newIndex;
}

我正在尝试计算newIndex值的平均值,基本上我不能do作为getNewIndex是String,并且可以有空字符串(值)。我无法将空值转换为0,因为newIndex中将有0个值。

I am trying to compute the average of the newIndex value, which basically I cannot do as getNewIndex is String, and there can be empty string(values). I cannot convert to the null values to 0 as there will be 0 values in newIndex.

是否有更简单的方法来计算平均值?

Is there an easier way to calculate the average?

例如。

mapOfIndicators = new HashMap<>();
List<IndicatorTableDTO> _tempValue = new ArrayList();
IndicatorTableDTO _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName1", 2012, "1.00");
_tempValue.add(_iTDTO);
_iTDTO = new IndicatorTableDTO("iName", "dName", "countryName2", 2012, "");
_tempValue.add(_iTDTO);
_iTDTO = new IndicatorTableDTO("iName", "dName", "countryName3", 2012, "0.02");
_tempValue.add(_iTDTO);
_iTDTO = new IndicatorTableDTO("iName", "dName", "countryName4", 2012, "-0.25");
_tempValue.add(_iTDTO);
_iTDTO = new IndicatorTableDTO("iName", "dName", "countryName5", 2012, "0.10");
_tempValue.add(_iTDTO);

mapOfIndicators.put("Test1", _tempValue);

   _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName1", 2012, "0.10");
   _tempValue.add(_iTDTO);
   _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName2", 2012, "", "", "");
   _tempValue.add(_iTDTO);
   _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName3", 2012, "", "", "");
   _tempValue.add(_iTDTO);
   _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName4", 2012, "0.25", "", "");
   _tempValue.add(_iTDTO);
   _iTDTO = new IndicatorTableDTO("iName", "dName", "countryName5", 2012, "1.10", "", "");
   _tempValue.add(_iTDTO);

   mapOfIndicators.put("Test2", _tempValue);

更新01:我们可以跳过空字符串/空值;平均值是每个countryName,所以我想找到相同contryName的newIndex的平均值,最终能够得到一个Map作为最终结果。

Update 01: We can skip empty string/null values; the average would be per countryName, so I want to find the average of the newIndex for same contryName and eventually be able to get a Map as the final result.

推荐答案

未经过测试,但应该做到这一点(或至少指导您解决方案):

Not tested, but should do the trick (or at least guide you towards the solution):

Map<String, Double> averageByCountryName = 
    map.values()
       .stream()
       .flatMap(list -> list.stream())
       .filter(objectDTO -> !objectDTO.getNewIndex().isEmpty())
       .collect(Collectors.groupingBy(
            ObjectDTO::getCountry,
            Collectors.averagingDouble(
                objectDTO -> Double.parseDouble(objectDTO.getNewIndex())));

这篇关于使用Lambda查找平均值(Double存储为String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆