I have the below code and I am using lamda expression to set the key and value pair of the hashMap to the list of Stats. I am new to stream API and I want to do it in stream API, can anyone help with this. Thanks.
final List<Stats> values = new ArrayList<>();
if (countMap != null) {
countMap.forEach((k, v) -> {
final Stats value = new Stats();
value.setType(k);
value.setCount(v);
final double percent = getPercentage(v, total.get());
value.setPercent(percent);
values.add(value);
});
}
private double getPercentage(final double count, final double total) {
final double percent = (count / (total * 1.0)) * 100;
final BigDecimal bd = new BigDecimal(percent).setScale(2, RoundingMode.HALF_UP);
return bd.doubleValue();
}