The list contains Arrays of ( String
, String
, BigDecimal
). I want to convert the List
to Map<String, Map<String, BigDecimal>>
grouped by the first String using java 8 Stream class.
List<Object[]> list = ss.createCriteria(PayrollDeduction.class)
.createAlias("payroll", "pay")
.add(Restrictions.le("pay.paymentDate", atDate))
.createAlias("payroll.employee", "employee")
.add(Restrictions.in("employee.id", Arrays.asList(empId)))
.setProjection(Projections.projectionList()
.add(Projections.property("gouvAgencyCode"))
.add(Projections.property("code"))
.add(Projections.sum("amount"))
.add(Projections.groupProperty("gouvAgencyCode"))
.add(Projections.groupProperty("code")))
.setReadOnly(true)
.list();
list.stream().collect(Collectors.groupingBy(r->r[0], Collectors.mapping(mapper, downstream)));
Do it like this:
List<Object[]> list = Arrays.asList(
new Object[] { "A", "X", new BigDecimal("1") },
new Object[] { "A", "Y", new BigDecimal("2.0") },
new Object[] { "B", "X", new BigDecimal("3.00") },
new Object[] { "C", "Z", new BigDecimal("4.000") }
);
Map<String, Map<String, BigDecimal>> map = list.stream()
.collect(Collectors.groupingBy(r -> (String) r[0],
Collectors.toMap(r -> (String) r[1],
r -> (BigDecimal) r[2])));
System.out.println(map);
Output
{A={X=1, Y=2.0}, B={X=3.00}, C={Z=4.000}}
To me, it looks like a XY Problem :
Hibernate's own ResultTransformer could have solved your problem in a cleaner way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.