java通过方法句柄实现getter方法从而对数组动态分组

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();
                        }
                }

        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值