public class TestReport {
public static void main(String[] args) {
Set<Integer> months = new HashSet<>();
Set<String> names = new HashSet<>();
List<Map<String, Object>> list = Lists.newArrayList(
new HashMap<String, Object>() {{
put("money", BigDecimal.valueOf(34593L));
put("name", "刘助理");
put("monthStr", 6);
}},
new HashMap<String, Object>() {{
put("money", BigDecimal.valueOf(34593L));
put("name", "刘助理");
put("monthStr", 7);
}},
new HashMap<String, Object>() {{
put("money", BigDecimal.valueOf(34593L));
put("name", "陈助理");
put("monthStr", 7);
}}
);
System.out.println(list);
List<Integer> month = new ArrayList<>();
Map<String, Object> result = new HashMap<>();
list.forEach(stringObjectMap -> {
// get month
months.add((Integer) stringObjectMap.get("monthStr"));
names.add((String) stringObjectMap.get("name"));
});
List<Dto> listDto = new ArrayList<>();
months.iterator().forEachRemaining(m -> {
Dto dto = new Dto();
dto.setMonth(m);
Map map = new HashMap<>();
list.forEach(stringObjectMap -> {
Integer mm = (Integer) stringObjectMap.get("monthStr");
if (m.intValue() == mm.intValue()) {
BigDecimal bg = dto.getAllMoney()==null?BigDecimal.ZERO:dto.getAllMoney();
dto.setAllMoney(bg.add((BigDecimal) stringObjectMap.get("money")));
map.put(stringObjectMap.get("name"), stringObjectMap.get("money"));
}
});
dto.setMap(map);
listDto.add(dto);
});
System.out.println(listDto.toString());
System.out.println("=======================================");
System.out.print("月份\t");
names.forEach(n->{
System.out.print(n+"\t");
});
System.out.print("工计\t");
System.out.println();
listDto.forEach(dto -> {
System.out.print(dto.getMonth()+"\t");
names.forEach(n->{
System.out.print(dto.getMap().get(n)+"\t");
});
System.out.print(dto.getAllMoney()+"\t");
System.out.println();
});
}
}
class Dto {
private Integer month;
private BigDecimal allMoney;
private Map map;
@Override
public String toString() {
return "Dto{" +
"month=" + month +
", allMoney=" + allMoney +
", map=" + map +
'}';
}
public Integer getMonth() {
return month;
}
public void setMonth(Integer month) {
this.month = month;
}
public BigDecimal getAllMoney() {
return allMoney;
}
public void setAllMoney(BigDecimal allMoney) {
this.allMoney = allMoney;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}