java list 按属性分组_java List按照对象的属性进行分组

1 //

2 // 分组 返回Map3 //4

5

6 packagecom.xxxx.dsc.application.utils;7

8 importjava.util.ArrayList;9 importjava.util.HashMap;10 importjava.util.HashSet;11 importjava.util.LinkedHashMap;12 importjava.util.List;13 importjava.util.Set;14 importorg.apache.commons.collections.CollectionUtils;15

16 public classHashlize {17 publicHashlize() {18 }19

20 public staticHashMap hashlizeObjects(Object[] objs, IHashKey iHashKey) {21 if (objs != null && objs.length != 0) {22 if (iHashKey == null) {23 throw new RuntimeException("iHashKey cann't be null");24 } else{25 HashMap result = newHashMap();26

27 for(int i = 0; i < objs.length; ++i) {28 String key =iHashKey.getKey(objs[i]);29 ArrayList al =(ArrayList)result.get(key);30 if (al == null) {31 al = newArrayList();32 result.put(key, al);33 }34

35 al.add(objs[i]);36 }37

38 returnresult;39 }40 } else{41 return null;42 }43 }44

45 public staticHashMap hashlizeObjects(List objs, IHashKey iHashKey) {46 if (objs != null && !objs.isEmpty()) {47 if (iHashKey == null) {48 throw new RuntimeException("iHashKey cann't be null");49 } else{50 HashMap result = newHashMap();51

52 for(int i = 0; i < objs.size(); ++i) {53 String key =iHashKey.getKey(objs.get(i));54 ArrayList al =(ArrayList)result.get(key);55 if (al == null) {56 al = newArrayList();57 result.put(key, al);58 }59

60 al.add(objs.get(i));61 }62

63 returnresult;64 }65 } else{66 return null;67 }68 }69

70 public staticLinkedHashMap hashlizeObjectsLinked(List objs, IHashKey iHashKey) {71 if (objs != null && !objs.isEmpty()) {72 if (iHashKey == null) {73 throw new RuntimeException("iHashKey cann't be null");74 } else{75 LinkedHashMap result = newLinkedHashMap();76

77 for(int i = 0; i < objs.size(); ++i) {78 String key =iHashKey.getKey(objs.get(i));79 ArrayList al =(ArrayList)result.get(key);80 if (al == null) {81 al = newArrayList();82 result.put(key, al);83 }84

85 al.add(objs.get(i));86 }87

88 returnresult;89 }90 } else{91 return null;92 }93 }94

95 public staticHashMap hashlizeMap(Object[] objs, IHashKey iHashKey) {96 if (objs != null && objs.length != 0) {97 if (iHashKey == null) {98 throw new RuntimeException("iHashKey cann't be null");99 } else{100 HashMap result = newHashMap();101

102 for(int i = 0; i < objs.length; ++i) {103 String key =iHashKey.getKey(objs[i]);104 result.put(key, objs[i]);105 }106

107 returnresult;108 }109 } else{110 return null;111 }112 }113

114 public staticHashMap hashlizeMap(List objs, IHashKey iHashKey) {115 if (objs != null && !objs.isEmpty()) {116 if (iHashKey == null) {117 throw new RuntimeException("iHashKey cann't be null");118 } else{119 HashMap result = newHashMap();120

121 for(int i = 0; i < objs.size(); ++i) {122 String key =iHashKey.getKey(objs.get(i));123 result.put(key, objs.get(i));124 }125

126 returnresult;127 }128 } else{129 return null;130 }131 }132

133 public staticHashMap hashlizeObjects(Object[] objs, IHashKey iHashKey, ICombine iCombine) {134 if (objs != null && objs.length != 0) {135 if (iHashKey == null) {136 throw new RuntimeException("iHashKey cann't be null");137 } else{138 HashMap result = newHashMap();139

140 for(int i = 0; i < objs.length; ++i) {141 String key =iHashKey.getKey(objs[i]);142 Object o =result.get(key);143 result.put(key, iCombine.combine(o, objs[i]));144 }145

146 returnresult;147 }148 } else{149 return null;150 }151 }152

153 public staticHashMap hashlizeObjects(List objs, IHashKey iHashKey, ICombine iCombine) {154 if (objs != null && !objs.isEmpty()) {155 if (iHashKey == null) {156 throw new RuntimeException("iHashKey cann't be null");157 } else{158 HashMap result = newHashMap();159 int size =objs.size();160

161 for(int i = 0; i < size; ++i) {162 String key =iHashKey.getKey(objs.get(i));163 Object o =result.get(key);164 result.put(key, iCombine.combine(o, objs.get(i)));165 }166

167 returnresult;168 }169 } else{170 return null;171 }172 }173

174 public staticLinkedHashMap linkedHashlizeObjects(List objs, IHashKey iHashKey, ICombine iCombine) {175 if (objs != null && !objs.isEmpty()) {176 if (iHashKey == null) {177 throw new RuntimeException("iHashKey cann't be null");178 } else{179 LinkedHashMap result = newLinkedHashMap();180 int size =objs.size();181

182 for(int i = 0; i < size; ++i) {183 String key =iHashKey.getKey(objs.get(i));184 Object o =result.get(key);185 result.put(key, iCombine.combine(o, objs.get(i)));186 }187

188 returnresult;189 }190 } else{191 return null;192 }193 }194

195 public static SethashlizeSet(List objs, IHashKey iHashKey) {196 if(CollectionUtils.isEmpty(objs)) {197 return null;198 } else if (iHashKey == null) {199 throw new RuntimeException("iHashKey cann't be null");200 } else{201 Set result = newHashSet();202

203 for(int i = 0; i < objs.size(); ++i) {204 String key =iHashKey.getKey(objs.get(i));205 result.add(key);206 }207

208 returnresult;209 }210 }211

212 public static ListhashlizeList(List objs, IHashKey iHashKey) {213 if(CollectionUtils.isEmpty(objs)) {214 return null;215 } else if (iHashKey == null) {216 throw new RuntimeException("iHashKey cann't be null");217 } else{218 List result = newArrayList();219

220 for(int i = 0; i < objs.size(); ++i) {221 String key =iHashKey.getKey(objs.get(i));222 result.add(key);223 }224

225 returnresult;226 }227 }228 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值