调用泛型对象方法List<T>并返回泛型结果Map <String,Object>,参数为具体对象集合,根据不同对象类型进行不同操作

 首先要对集合处理,取集合其中某几条的数据,业务方法getRequireMap()。

然后调用公共方法getChoosePlansMap(),公共方法可能是不同对象集合。到方法里再根据传入的对象类型,实现不同的处理逻辑。

遍历集合,获取参数的实例化对象desObj。

Object desObj = cla.newInstance();

根据不同类型进行操作

if(desObj.getClass().equals(InsopDeliveryPlanBO.class)){
.....
}

返回泛型map,

Map<String, T> map=new HashMap<>();
map.put(insopDeliveryPlanBOList.get(i).getId(),(T)insopDeliveryPlanBOList.get(i));

最后对泛型map进行实例化转换操作。

Map<String, InsopDeliveryPlanBO> map = getChoosePlansMap(InsopDeliveryPlanBO.class,insopDeliveryPlanBOList, chooseId).entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey, e -> (InsopDeliveryPlanBO)e.getValue()));

main方法实现如下:

public static void main(String[] args) {
        List<InsopDeliveryPlanBO> insopDeliveryPlanBOList = new ArrayList<InsopDeliveryPlanBO>();
        InsopDeliveryPlanBO planBO1 = new InsopDeliveryPlanBO();
			planBO1.setId("001");
			planBO1.setContractId("1111");
			planBO1.setDeliveryDate("2022-09-03");
			planBO1.setInterestCapital(10000d);
			planBO1.setStartDate("2020-09-03");
			planBO1.setEndDate("2020-09-21");
			planBO1.setInterestDays(18);
			planBO1.setActualRate(0.02d);
			planBO1.setDeliveryAmount(123.34d);
			planBO1.setPlanDeliveryType(1);
			planBO1.setExecutionStatus(2);
            planBO1.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.DELIVERY);
        insopDeliveryPlanBOList.add(planBO1);
        InsopDeliveryPlanBO planBO2 = new InsopDeliveryPlanBO();
			planBO2.setId("002");
			planBO2.setContractId("1111");
			planBO2.setDeliveryDate("2022-09-21");
			planBO2.setInterestCapital(10000d);
			planBO2.setStartDate("2020-09-21");
			planBO2.setEndDate("2020-10-21");
			planBO2.setInterestDays(18);
			planBO2.setActualRate(0.02d);
			planBO2.setDeliveryAmount(123.34d);
			planBO2.setPlanDeliveryType(1);
			planBO2.setExecutionStatus(2);
            planBO2.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.INTEREST);
        insopDeliveryPlanBOList.add(planBO2);
        InsopDeliveryPlanBO planBO3 = new InsopDeliveryPlanBO();
			planBO3.setId("003");
			planBO3.setContractId("1111");
			planBO3.setDeliveryDate("2022-09-21");
			planBO3.setInterestCapital(10000d);
			planBO3.setStartDate("2020-10-21");
			planBO3.setEndDate("2020-11-21");
			planBO3.setInterestDays(18);
			planBO3.setActualRate(0.02d);
			planBO3.setDeliveryAmount(123.34d);
			planBO3.setPlanDeliveryType(1);
			planBO3.setExecutionStatus(2);
            planBO3.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.INTEREST);
        insopDeliveryPlanBOList.add(planBO3);
        InsopDeliveryPlanBO planBO4 = new InsopDeliveryPlanBO();
			planBO4.setId("004");
			planBO4.setContractId("1111");
			planBO4.setDeliveryDate("2022-09-03");
			planBO4.setInterestCapital(10000d);
			planBO4.setStartDate("2020-11-21");
			planBO4.setEndDate("2020-12-23");
			planBO4.setInterestDays(18);
			planBO4.setActualRate(0.02d);
			planBO4.setDeliveryAmount(123.34d);
			planBO4.setPlanDeliveryType(1);
			planBO4.setExecutionStatus(2);
            planBO4.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.INTEREST);
        insopDeliveryPlanBOList.add(planBO4);
        InsopDeliveryPlanBO planBO5 = new InsopDeliveryPlanBO();
			planBO5.setId("005");
			planBO5.setContractId("1111");
			planBO5.setDeliveryDate("2022-09-03");
			planBO5.setInterestCapital(10000d);
			planBO5.setStartDate("2020-12-23");
			planBO5.setEndDate("2021-01-21");
			planBO5.setInterestDays(18);
			planBO5.setActualRate(0.02d);
			planBO5.setDeliveryAmount(123.34d);
			planBO5.setPlanDeliveryType(1);
			planBO5.setExecutionStatus(2);
            planBO5.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.DELIVERY);
        insopDeliveryPlanBOList.add(planBO5);
        InsopDeliveryPlanBO planBO6 = new InsopDeliveryPlanBO();
			planBO6.setId("006");
			planBO6.setContractId("1111");
			planBO6.setDeliveryDate("2022-09-03");
			planBO6.setInterestCapital(10000d);
			planBO6.setStartDate("2021-01-21");
			planBO6.setEndDate("2021-02-22");
			planBO6.setInterestDays(18);
			planBO6.setActualRate(0.02d);
			planBO6.setDeliveryAmount(123.34d);
			planBO6.setPlanDeliveryType(1);
			planBO6.setExecutionStatus(2);
            planBO6.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.INTEREST);
        insopDeliveryPlanBOList.add(planBO6);
        InsopDeliveryPlanBO planBO7 = new InsopDeliveryPlanBO();
        planBO7.setId("007");
        planBO7.setContractId("1111");
        planBO7.setDeliveryDate("2022-09-03");
        planBO7.setInterestCapital(10000d);
        planBO7.setStartDate("2021-02-22");
        planBO7.setEndDate("2021-03-03");
        planBO7.setInterestDays(18);
        planBO7.setActualRate(0.02d);
        planBO7.setDeliveryAmount(123.34d);
        planBO7.setPlanDeliveryType(1);
        planBO7.setExecutionStatus(2);
        planBO7.setDeliveryPlanType(FxdvConstant.FxdvDeliveryPlanBusinessType.DELIVERY);
        insopDeliveryPlanBOList.add(planBO7);
        System.out.println("insopDeliveryPlanBOList.size():"+insopDeliveryPlanBOList.size());
        //选中005
        String chooseId="005";
        List<InsopDeliveryPlanBO> list = new ArrayList<InsopDeliveryPlanBO>();
        Map<String, InsopDeliveryPlanBO> map = getChoosePlansMap(InsopDeliveryPlanBO.class,insopDeliveryPlanBOList, chooseId).entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> (InsopDeliveryPlanBO)e.getValue()));

//        list.stream().forEach(System.out::println);
        System.out.println(JSON.toJSONString(map));


       
    }
/**
    * @Description: insopDeliveryPlanBOList 需要截取的交割计划 chooseId 从哪条交割计划开始往上截取对应的交割及利息,传这条交割计划的id
    * @Param: [insopDeliveryPlanBOList, chooseId]
    * @return: java.util.Map<java.lang.String,com.iss.cms.fxdv.insop.delivery.service.bo.InsopDeliveryPlanBO>
    * @Date: 2022/9/8
    */
    private static <T> Map<String, T> getChoosePlansMap(Class cla, List sourceList, String chooseId) {
        Map<String, T> map=new HashMap<>();
        List<InsopDeliveryPlanBO> insopDeliveryPlanBOList = new ArrayList();
        List<IsDeliveryPlanBO> IsDeliveryPlanBOList = new ArrayList();
        System.out.println("");
        if (sourceList != null) {
            for(int i = 0; i < sourceList.size(); ++i) {
                try {
                    Object desObj = cla.newInstance();
                    System.out.println("desObj"+desObj);
                    if(desObj.getClass().equals(InsopDeliveryPlanBO.class)){
                        InsopDeliveryPlanBO sourceObj = (InsopDeliveryPlanBO)sourceList.get(i);
                        insopDeliveryPlanBOList.add(sourceObj);
//                        getRequireMap(chooseId, map, insopDeliveryPlanBOList);
                    }else if(desObj.getClass().equals(IsDeliveryPlanBO.class)){
                        IsDeliveryPlanBO sourceObj = (IsDeliveryPlanBO)sourceList.get(i);
                        IsDeliveryPlanBOList.add(sourceObj);
//                        getRequireMap(chooseId, map, IsDeliveryPlanBOList);
                    }else{
                        return null;
                    }

                } catch (Exception var6) {
                    LOGGER.error("list copy error", var6);
                    throw new RuntimeException("list copy error", var6);
                }
            }
        }
        if(cla.equals(InsopDeliveryPlanBO.class)){
            getRequireMap(chooseId, map, insopDeliveryPlanBOList);
        }else if(cla.equals(IsDeliveryPlanBO.class)){
//                        getRequireMap(chooseId, map, IsDeliveryPlanBOList);
        }else{
            return null;
        }


        return map;
    }


    private static <T> void getRequireMap(String chooseId, Map<String, T> map, List<InsopDeliveryPlanBO> insopDeliveryPlanBOList) {
        int index=0;
        for (int i = insopDeliveryPlanBOList.size()-1; i >=0; i--) {
            if(index!=0){
                if(insopDeliveryPlanBOList.get(i).getDeliveryPlanType()== FxdvConstant.FxdvDeliveryPlanBusinessType.DELIVERY){
                    break;
                }else{
//                    list.add(insopDeliveryPlanBOList.get(i));
                    map.put(insopDeliveryPlanBOList.get(i).getId(),(T)insopDeliveryPlanBOList.get(i));
                }
            }else{
                if(insopDeliveryPlanBOList.get(i).getId().equalsIgnoreCase(chooseId)){
//                    list.add(insopDeliveryPlanBOList.get(i));
                    map.put(insopDeliveryPlanBOList.get(i).getId(),(T)insopDeliveryPlanBOList.get(i));
                    index=i;
                }
            }
            if(i==0)
                break;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmlcq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值