为Map求和的有效方法

时间:2012-09-27 11:03:23

标签: map enums

我有20个字段的枚举。 我还必须循环一个列表并为每一行获取 与要放入地图的枚举对应的字段:

pseudo code ex
public enum TypeCodes {
    buy
    sell
    call
    etc... (17 other codes)
}

//here's where I need help for something more efficient
getMapOfTypeCodes(list) {

BigDecimal cashShares = new BigDecimal(0);
BigDecimal buyShares = new BigDecimal(0);
ect... 18 other variables...

for each row in list {

    switch row.getTypeCode
    case buy:
        buyShares = row.getShares();
        break;
    case sell:
        sellShares = row.getShares();
        break;
    etc... 18 other statements
}
Map<TypeCode, BigDecimal)> codeMap = new HashMap<TypeCode, BigDecimal) >();
codeMap.put(TypeCode.BUY, buyShares);
codeMap.put(TypeCode.SELL, sellShares);
ect... repeat 18 times...

return codeMap
}

我确信有一种更有效的方式来通过枚举并将这些字段相加而不是使用20个差异变量?请帮忙。感谢。

0 个答案:

没有答案