拓展集合工具类CollectionUtils,供大家参考,具体内容如下
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
package com.demo.utils; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.demo.bean.EmployeeEntity; /** * * <p>自定义集合工具类拓展常用方法</p> * @autho 董杨炀 * @time 2017-4-10 上午11:33:36 */ public class CollectionUtils extends org.apache.commons.collections.CollectionUtils { private static final Logger LOGGER = LoggerFactory.getLogger(CollectionUtils. class ); private static final int DEFAULT_SIZE = 1000 ; /** * * <p>拆分List为固定大小的多个集合</p> * <p>推荐使用</p> * <p>返回集合的size越小,此方法性能越高</p> * @param baseList * @param size * @return ArrayList * @autho 董杨炀 * @time 2017-4-10 上午11:30:43 */ @SuppressWarnings ( "unchecked" ) public static <T> List<List<T>> fastSplitList(List<T> baseList, int size) { if (baseList == null || baseList.size() == 0 ) { return null ; } if (size <= 0 ) { size = DEFAULT_SIZE; } int arrSize = baseList.size() % size == 0 ? baseList.size() / size : baseList.size() / size + 1 ; List<List<T>> resultList = new ArrayList<List<T>>(); for ( int i = 0 ; i < arrSize; i++) { if (arrSize - 1 == i) { resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, baseList.size()))); } else { resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, size * (i + 1 )))); } } return resultList; } /** * * <p>拆分List为固定大小的多个集合</p> * <p>返回集合的size越大,此方法性能越高</p> * @param baseList * @param size * @return ArrayList * @autho 董杨炀 * @time 2017-4-10 上午11:30:43 */ public static <T> List<List<T>> splitList(List<T> baseList, int size) { if (baseList == null || baseList.size() == 0 ) { return null ; } if (size <= 0 ) { size = DEFAULT_SIZE; } List<List<T>> resultList = new ArrayList<List<T>>(); for ( int i = 0 ; i < baseList.size(); ++i) { if (i % size == 0 ) { List<T> result = new ArrayList<T>(); resultList.add(result); } resultList.get(i / size).add(baseList.get(i)); } return resultList; } /** * * <p>集合转Set</p> * @param coll 源集合 * @param keyType 属性类型 * @param keyMethodName 属性get方法 * @return LinkedHashSet * @autho 董杨炀 * @time 2017-4-10 上午11:31:50 */ public static <K, V> Set<K> asSet( final java.util.Collection<V> coll, final Class<K> keyType , final String keyMethodName) { if (CollectionUtils.isEmpty(coll)) { return new HashSet<K>( 0 ); } final Set<K> set = new LinkedHashSet<K>(coll.size()); try { for ( final V value : coll) { Object object; Method method = value.getClass().getMethod(keyMethodName); object = method.invoke(value); @SuppressWarnings ( "unchecked" ) final K key = (K) object; set.add(key); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new CollectionUtilsException( "Collection conversion Set exceptions" ); } return set; } /** * * <p>集合转Map</p> * <p>比如:List<EmployeeEntity>,讲EmployeeEntity的name属性作为key,转换成Map</p> * @param coll 源集合 * @param keyType 属性类型 * @param valueType 源数据类型(实体类型) * @param keyMethodName 属性get方法 * @return LinkedHashMap * @autho 董杨炀 * @time 2017-4-10 上午11:32:01 */ public static <K, V> Map<K, V> asMap( final java.util.Collection<V> coll, final Class<K> keyType , final Class<V> valueType, final String keyMethodName) { if (CollectionUtils.isEmpty(coll)) { return new LinkedHashMap<K, V>( 0 ); } final Map<K, V> map = new LinkedHashMap<K, V>(coll.size()); try { Method method = valueType.getMethod(keyMethodName); for ( final V value : coll) { Object object; object = method.invoke(value); @SuppressWarnings ( "unchecked" ) final K key = (K) object; map.put(key, value); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new CollectionUtilsException( "Collection conversion Map exceptions" ); } return map; } /** * * <p>集合转List</p> * @param coll * @return ArrayList * @autho 董杨炀 * @time 2017-4-10 上午11:32:56 */ public static <V> List<V> asList( final java.util.Collection<V> coll) { if (CollectionUtils.isEmpty(coll)) { return new ArrayList<V>( 0 ); } final List<V> list = new ArrayList<V>(); for ( final V value : coll) { if (value != null ) { list.add(value); } } return list; } /** * <p>集合<String>toString</p> * @param collection 泛型必须为String类型 * @param split 比如连接符"," * @return * @autho 董杨炀 * @time 2017-4-10 下午3:22:24 */ public static String collToString(Collection<String> collection, String split) { StringBuilder sb = new StringBuilder(); if (collection != null ) { int i = 0 , size = collection.size(); for (Iterator<String> iterator = collection.iterator(); iterator.hasNext();) { String str = iterator.next(); sb.append(str); if (++i < size) { sb.append(split); } } } return sb.toString(); } static class CollectionUtilsException extends RuntimeException{ private static final long serialVersionUID = 1L; public CollectionUtilsException(String s) { super (s); } public CollectionUtilsException(String s, Throwable e) { super (s, e); } public CollectionUtilsException(Throwable e) { super (e); } } public static void main(String[] args) { List<String> baseList = new ArrayList<String>( 1000000 ); for ( int i = 0 ; i < 1000000 ; i++) { baseList.add( "data:" + " i" ); } long currentTimeMillis1 = System.currentTimeMillis(); List<List<String>> splitList = splitList(baseList, 1000 ); long currentTimeMillis2 = System.currentTimeMillis(); System.out.println(splitList.size()); System.out.println( "切割完成时间为:" +String.valueOf(currentTimeMillis2-currentTimeMillis1)+ "毫秒" ); long currentTimeMillis3 = System.currentTimeMillis(); List<List<String>> newList = fastSplitList(baseList, 1000 ); long currentTimeMillis4 = System.currentTimeMillis(); System.out.println(newList.size()); System.out.println( "快速切割完成时间为:" +String.valueOf(currentTimeMillis4-currentTimeMillis3)+ "毫秒" ); List<EmployeeEntity> employeeList = new ArrayList<EmployeeEntity>(); for ( int i = 1 ; i < 11 ; i++) { EmployeeEntity entity = new EmployeeEntity(); entity.setName( "名字" +String.valueOf(i)); employeeList.add(entity); } System.out.println(employeeList.size()); Set<String> set = CollectionUtils.asSet(employeeList, String. class , "getName" ); for (String name : set) { System.out.println(name); } Map<String, EmployeeEntity> map = CollectionUtils.asMap(employeeList, String. class , EmployeeEntity. class , "getName" ); Set<String> keySet = map.keySet(); for (String key : keySet) { System.out.println(key); System.out.println(map.get(key)); } List<EmployeeEntity> list = CollectionUtils.asList(map.values()); for (EmployeeEntity employeeEntity : list) { System.out.println(employeeEntity); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_35712358/article/details/70254565