我是靠谱客的博主 怡然电脑,这篇文章主要介绍调用泛型对象方法List<T>并返回泛型结果Map <String,Object>,参数为具体对象集合,根据不同对象类型进行不同操作,现在分享给大家,希望可以做个参考。

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

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

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

复制代码
1
Object desObj = cla.newInstance();

根据不同类型进行操作

复制代码
1
2
3
if(desObj.getClass().equals(InsopDeliveryPlanBO.class)){ ..... }

返回泛型map,

复制代码
1
Map<String, T> map=new HashMap<>();
复制代码
1
map.put(insopDeliveryPlanBOList.get(i).getId(),(T)insopDeliveryPlanBOList.get(i));

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

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

main方法实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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; } }

最后

以上就是怡然电脑最近收集整理的关于调用泛型对象方法List<T>并返回泛型结果Map <String,Object>,参数为具体对象集合,根据不同对象类型进行不同操作的全部内容,更多相关调用泛型对象方法List<T>并返回泛型结果Map内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部