javaList按照对象的属性进⾏分组
⼀、问题背景
在java的开发过程中,我们往往也需要⽤对象进⾏分组进⾏处理,如何对List进⾏分组呢?以下我们简单⾼效的实现分组
⼆、问题解决
1 //以下WarehouseDeliveryTimeVO的列表中对其属性logisticsModel(物流模式)进⾏分组,new String[]{}可以按照多个属性进⾏分组,allTimeVO为其对象列表
2 Map<String,List<WarehouseDeliveryTimeVO>> logisticsModelGoodsMap = Hashlize.hashlizeObjects( allTimeVO,new HashKeyAdapter(new String[]{"logisticsModel"})); 3 //通过迭代可以获取分组后的结果
4 for(String key : logisticsModelGoodsMap.keySet()){
5 List<WarehouseDeliveryTimeVO> timeVO = (key);
6 //以下是获取分组后列表的其他业务操作
7 //checkTimeCross(timeVO);
8 }
当然我们也可以按照多个对象的属性进⾏分组,⽅法就是在 new String[]{"属性⼀","属性⼆"},即可按照所设置的两个属性进⾏分组,简单,⾼效。
⽤作公共类超棒!!
涉及的接⼝ IHashKey
dsc.application.utils;
2
3public interface IHashKey {
4 String getKey(Object var1);
5 }
接⼝ ICombine
dsc.application.utils;
2
3public interface ICombine {
4 Object combine(Object var1, Object var2);
5 }
类 HashKeyAdapter
dsc.application.utils;
2
3import flect.InvocationTargetException;
4import org.apachemons.beanutils.PropertyUtils;
5import org.apachemons.lang3.StringUtils;
6
7public class HashKeyAdapter implements IHashKey {
8 String[] m_KeyFields;
9static final String m_FixKey = "FIXKEY#$@";
10
11public HashKeyAdapter(String[] keyFields) {
12this.m_KeyFields = keyFields;
13 }
14
15public String getKey(Object o) {
16if (this.m_KeyFields != null && this.m_KeyFields.length != 0) {
17 String key = "";
18
19for(int i = 0; i < this.m_KeyFields.length; ++i) {
20try {
21if (StringUtils.isNotEmpty("" + SimpleProperty(o, this.m_KeyFields[i]))) {
22 Object v = SimpleProperty(o, this.m_KeyFields[i]);
23 key = key + (v == null ? "" : v + "");
24 }
25 } catch (IllegalAccessException var5) {
26throw new Message(), var5);
27 } catch (InvocationTargetException var6) {
28throw new Message(), var6);
29 } catch (NoSuchMethodException var7) {
30throw new Message(), var7);
31 }
32 }
33
34return key;
35 } else {
36return "FIXKEY#$@";
37 }
38 }
39 }
1//
2// 分组返回Map
3// 4
5
dsc.application.utils;
7
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.LinkedHashMap;
12import java.util.List;
13import java.util.Set;
14import llections.CollectionUtils;
15
16public class Hashlize {
17public Hashlize() {
18 }
19
20public static HashMap hashlizeObjects(Object[] objs, IHashKey iHashKey) {
21if (objs != null && objs.length != 0) {
22if (iHashKey == null) {
23throw new RuntimeException("iHashKey cann't be null");
24 } else {
25 HashMap result = new HashMap();
26
27for(int i = 0; i < objs.length; ++i) {
28 String key = Key(objs[i]);
29 ArrayList al = ((key);
30if (al == null) {
31 al = new ArrayList();
32 result.put(key, al);
33 }
34
35 al.add(objs[i]);
36 }
37
38return result;
39 }
40 } else {
41return null;
42 }
43 }
44
45public static HashMap hashlizeObjects(List objs, IHashKey iHashKey) {
46if (objs != null && !objs.isEmpty()) {
47if (iHashKey == null) {
48throw new RuntimeException("iHashKey cann't be null");
49 } else {
50 HashMap result = new HashMap();
51
52for(int i = 0; i < objs.size(); ++i) {
53 String key = (i));
54 ArrayList al = ((key);
55if (al == null) {
56 al = new ArrayList();
57 result.put(key, al);
58 }
59
60 al.(i));
61 }
62
63return result;
64 }
65 } else {
66return null;
67 }
68 }
69
70public static LinkedHashMap hashlizeObjectsLinked(List objs, IHashKey iHashKey) { 71if (objs != null && !objs.isEmpty()) {
72if (iHashKey == null) {
73throw new RuntimeException("iHashKey cann't be null");
74 } else {
75 LinkedHashMap result = new LinkedHashMap();
76
java arraylist用法77for(int i = 0; i < objs.size(); ++i) {
78 String key = (i));
79 ArrayList al = ((key);
80if (al == null) {
81 al = new ArrayList();
82 result.put(key, al);
83 }
84
85 al.(i));
87
88return result;
89 }
90 } else {
91return null;
92 }
93 }
94
95public static HashMap hashlizeMap(Object[] objs, IHashKey iHashKey) {
96if (objs != null && objs.length != 0) {
97if (iHashKey == null) {
98throw new RuntimeException("iHashKey cann't be null");
99 } else {
100 HashMap result = new HashMap();
101
102for(int i = 0; i < objs.length; ++i) {
103 String key = Key(objs[i]);
104 result.put(key, objs[i]);
105 }
106
107return result;
108 }
109 } else {
110return null;
111 }
112 }
113
114public static HashMap hashlizeMap(List objs, IHashKey iHashKey) {
115if (objs != null && !objs.isEmpty()) {
116if (iHashKey == null) {
117throw new RuntimeException("iHashKey cann't be null");
118 } else {
119 HashMap result = new HashMap();
120
121for(int i = 0; i < objs.size(); ++i) {
122 String key = (i));
123 result.put(key, (i));
124 }
125
126return result;
127 }
128 } else {
129return null;
130 }
131 }
132
133public static HashMap hashlizeObjects(Object[] objs, IHashKey iHashKey, ICombine iCombine) { 134if (objs != null && objs.length != 0) {
135if (iHashKey == null) {
136throw new RuntimeException("iHashKey cann't be null");
137 } else {
138 HashMap result = new HashMap();
139
140for(int i = 0; i < objs.length; ++i) {
141 String key = Key(objs[i]);
142 Object o = (key);
143 result.put(key, iCombinebine(o, objs[i]));
144 }
145
146return result;
147 }
148 } else {
149return null;
150 }
151 }
152
153public static HashMap hashlizeObjects(List objs, IHashKey iHashKey, ICombine iCombine) { 154if (objs != null && !objs.isEmpty()) {
155if (iHashKey == null) {
156throw new RuntimeException("iHashKey cann't be null");
157 } else {
158 HashMap result = new HashMap();
159int size = objs.size();
160
161for(int i = 0; i < size; ++i) {
162 String key = (i));
163 Object o = (key);
164 result.put(key, iCombinebine(o, (i)));
165 }
166
167return result;
168 }
169 } else {
170return null;
171 }
172 }
173
174public static LinkedHashMap linkedHashlizeObjects(List objs, IHashKey iHashKey, ICombine iCombine) { 175if (objs != null && !objs.isEmpty()) {
176if (iHashKey == null) {
177throw new RuntimeException("iHashKey cann't be null");
178 } else {
179 LinkedHashMap result = new LinkedHashMap();
180int size = objs.size();
181
182for(int i = 0; i < size; ++i) {
183 String key = (i));
184 Object o = (key);
185 result.put(key, iCombinebine(o, (i)));
186 }
187
188return result;
189 }
190 } else {
191return null;
192 }
193 }
194
195public static Set<String> hashlizeSet(List objs, IHashKey iHashKey) {
196if (CollectionUtils.isEmpty(objs)) {
197return null;
198 } else if (iHashKey == null) {
199throw new RuntimeException("iHashKey cann't be null");
200 } else {
201 Set<String> result = new HashSet();
202
203for(int i = 0; i < objs.size(); ++i) {
204 String key = (i));
205 result.add(key);
206 }
207
208return result;
209 }
210 }
211
212public static List<String> hashlizeList(List objs, IHashKey iHashKey) {
213if (CollectionUtils.isEmpty(objs)) {
214return null;
215 } else if (iHashKey == null) {
216throw new RuntimeException("iHashKey cann't be null");
217 } else {
218 List<String> result = new ArrayList();
219
220for(int i = 0; i < objs.size(); ++i) {
221 String key = (i));
222 result.add(key);
223 }
224
225return result;
226 }
227 }
228 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论