import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class PayFocusManager<T> {
public static Map<String, Map<String, List<BillChannelBo>>> secondGroup(List<BillChannelBo> bills, Integer focusChannel) {
Focus focus = Focus.find(focusChannel);
if (Objects.isNull(focus)) {
return null;
}
final MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
MethodHandle handle = lookup.findVirtual(BillChannelBo.class, focus.getField(), MethodType.methodType(String.class));
return bills.stream().collect(Collectors.groupingBy(BillChannelBo::getBusinessType, Collectors.groupingBy(
d -> {
try {
return (String) handle.invokeExact(d);
} catch (Throwable e) {
throw new RuntimeException(e);
}
})
));
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
List<BillChannelBo> list = new ArrayList<>();
BillChannelBo b1 = new BillChannelBo();
b1.setBusinessType("20001");
b1.setOrderChannel("1101");
b1.setTotalPayAmount(new BigDecimal(123));
BillChannelBo b5 = new BillChannelBo();
b5.setBusinessType("20001");
b5.setOrderChannel("1101");
b5.setTotalPayAmount(new BigDecimal(1234));
BillChannelBo b2 = new BillChannelBo();
b2.setBusinessType("20001");
b2.setOrderChannel("1102");
b2.setTotalPayAmount(new BigDecimal(321));
BillChannelBo b6 = new BillChannelBo();
b6.setBusinessType("20001");
b6.setOrderChannel("1102");
b6.setTotalPayAmount(new BigDecimal(4321));
BillChannelBo b3 = new BillChannelBo();
b3.setBusinessType("20002");
b3.setOrderChannel("1101");
b3.setTotalPayAmount(new BigDecimal(456));
BillChannelBo b7 = new BillChannelBo();
b7.setBusinessType("20002");
b7.setOrderChannel("1101");
b7.setTotalPayAmount(new BigDecimal(4567));
BillChannelBo b4 = new BillChannelBo();
b4.setBusinessType("20002");
b4.setOrderChannel("1102");
b4.setTotalPayAmount(new BigDecimal(654));
BillChannelBo b8 = new BillChannelBo();
b8.setBusinessType("20002");
b8.setOrderChannel("1102");
b8.setTotalPayAmount(new BigDecimal(6543));
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
list.add(b6);
list.add(b7);
list.add(b8);
Map<String, Map<String, List<BillChannelBo>>> stringMapMap = secondGroup(list,2);
for (String s : stringMapMap.keySet()) {
for (String s1 : stringMapMap.get(s).keySet()) {
System.out.println(s);
System.out.println(s1);
for (BillChannelBo billChannelBo : stringMapMap.get(s).get(s1)) {
System.out.println(JsonUtil.toJson(billChannelBo));
}
System.out.println();
}
}
}
}
java通过方法句柄实现getter方法从而对数组动态分组
于 2022-03-17 16:30:00 首次发布