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

一、问题背景

在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 = logisticsModelGoodsMap.get(key);
6           //以下是获取分组后列表的其他业务操作
7              //checkTimeCross(timeVO);
8          }

当然我们也可以按照多个对象的属性进行分组,方法就是在  new String[]{"属性一","属性二"},即可按照所设置的两个属性进行分组,简单,高效。

用作公共类 超棒!!

 涉及的接口 IHashKey 

1 package com.xxxx.dsc.application.utils;
2 
3 public interface IHashKey {
4     String getKey(Object var1);
5 }

接口 ICombine 

1 package com.xxxx.dsc.application.utils;
2 
3 public interface ICombine {
4     Object combine(Object var1, Object var2);
5 }

类 HashKeyAdapter 

 1 package com.xxxx.dsc.application.utils;
 2 
 3 import java.lang.reflect.InvocationTargetException;
 4 import org.apache.commons.beanutils.PropertyUtils;
 5 import org.apache.commons.lang3.StringUtils;
 6 
 7 public class HashKeyAdapter implements IHashKey {
 8     String[] m_KeyFields;
 9     static final String m_FixKey = "FIXKEY#$@";
10 
11     public HashKeyAdapter(String[] keyFields) {
12         this.m_KeyFields = keyFields;
13     }
14 
15     public String getKey(Object o) {
16         if (this.m_KeyFields != null && this.m_KeyFields.length != 0) {
17             String key = "";
18 
19             for(int i = 0; i < this.m_KeyFields.length; ++i) {
20                 try {
21                     if (StringUtils.isNotEmpty("" + PropertyUtils.getSimpleProperty(o, this.m_KeyFields[i]))) {
22                         Object v = PropertyUtils.getSimpleProperty(o, this.m_KeyFields[i]);
23                         key = key + (v == null ? "" : v + "");
24                     }
25                 } catch (IllegalAccessException var5) {
26                     throw new RuntimeException(var5.getMessage(), var5);
27                 } catch (InvocationTargetException var6) {
28                     throw new RuntimeException(var6.getMessage(), var6);
29                 } catch (NoSuchMethodException var7) {
30                     throw new RuntimeException(var7.getMessage(), var7);
31                 }
32             }
33 
34             return key;
35         } else {
36             return "FIXKEY#$@";
37         }
38     }
39 }

类 Hashlize 

  1 //
  2 // 分组 返回Map  
3
// 4
5
6 package com.xxxx.dsc.application.utils; 7 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.HashSet; 11 import java.util.LinkedHashMap; 12 import java.util.List; 13 import java.util.Set; 14 import org.apache.commons.collections.CollectionUtils; 15 16 public class Hashlize { 17 public Hashlize() { 18 } 19 20 public static HashMap 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 = new HashMap(); 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 = new ArrayList(); 32 result.put(key, al); 33 } 34 35 al.add(objs[i]); 36 } 37 38 return result; 39 } 40 } else { 41 return null; 42 } 43 } 44 45 public static HashMap 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 = new HashMap(); 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 = new ArrayList(); 57 result.put(key, al); 58 } 59 60 al.add(objs.get(i)); 61 } 62 63 return result; 64 } 65 } else { 66 return null; 67 } 68 } 69 70 public static LinkedHashMap 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 = new LinkedHashMap(); 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 = new ArrayList(); 82 result.put(key, al); 83 } 84 85 al.add(objs.get(i)); 86 } 87 88 return result; 89 } 90 } else { 91 return null; 92 } 93 } 94 95 public static HashMap 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 = new HashMap(); 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 return result; 108 } 109 } else { 110 return null; 111 } 112 } 113 114 public static HashMap 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 = new HashMap(); 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 return result; 127 } 128 } else { 129 return null; 130 } 131 } 132 133 public static HashMap 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 = new HashMap(); 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 return result; 147 } 148 } else { 149 return null; 150 } 151 } 152 153 public static HashMap 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 = new HashMap(); 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 return result; 168 } 169 } else { 170 return null; 171 } 172 } 173 174 public static LinkedHashMap 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 = new LinkedHashMap(); 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 return result; 189 } 190 } else { 191 return null; 192 } 193 } 194 195 public static Set<String> hashlizeSet(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<String> result = new HashSet(); 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 return result; 209 } 210 } 211 212 public static List<String> hashlizeList(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<String> result = new ArrayList(); 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 return result; 226 } 227 } 228 }

原文地址:https://www.cnblogs.com/zluckiy/p/12571715.html