JAVA集合Listグループ分けの2つの方法
http://blog.csdn.net/yangxiaojun9238/article/details/51500934
1.工具類
1.工具類
"code" class="java">import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.framework.util.ParamUtils;
public class CommonUtils {
/**
* , ,
*
* @author ZhangLiKun
* @title GroupBy
* @date 2013-4-23
*/
public interface GroupBy {
T groupby(Object obj);
}
/**
*
* @param colls
* @param gb
* @return
*/
public static final extends Comparable, D> Map> group(Collection colls, GroupBy gb) {
if (colls == null || colls.isEmpty()) {
System.out.println(" !");
return null;
}
if (gb == null) {
System.out.println(" Null!");
return null;
}
Iterator iter = colls.iterator();
Map> map = new HashMap>();
while (iter.hasNext()) {
D d = iter.next();
T t = gb.groupby(d);
if (map.containsKey(t)) {
map.get(t).add(d);
} else {
List list = new ArrayList();
list.add(d);
map.put(t, list);
}
}
return map;
}
/**
* List V methodName ( K ) , Map>
* method V , K
*
* @param list
*
* @param map
* map
* @param clazz
* V
* @param methodName
*
*/
public static void listGroup2Map(List list, Map> map, Class clazz, String methodName) {
//
if (null == list || null == map || null == clazz || !ParamUtils.chkString(methodName)) {
System.out.print("CommonUtils.listGroup2Map ,list:" + list + " ;map:" + map + " ;clazz:" + clazz + " ;methodName:" + methodName);
return;
}
//
Method method = getMethodByName(clazz, methodName);
//
if (null == method) {
return;
}
//
listGroup2Map(list, map, method);
}
/**
* ,
*
* @param clazz
* @param methodName
* @return
*/
public static Method getMethodByName(Class> clazz, String methodName) {
Method method = null;
//
if (null == clazz || !ParamUtils.chkString(methodName)) {
System.out.print("CommonUtils.getMethodByName ,clazz:" + clazz + " ;methodName:" + methodName);
return method;
}
try {
method = clazz.getDeclaredMethod(methodName);
} catch (Exception e) {
System.out.print(" !");
}
return method;
}
/**
* List V ( K ) , Map>
* method V , K
*
* @param list
*
* @param map
* map
* @param method
*
*/
@SuppressWarnings("unchecked")
public static void listGroup2Map(List list, Map> map, Method method) {
//
if (null == list || null == map || null == method) {
System.out.print("CommonUtils.listGroup2Map ,list:" + list + " ;map:" + map + " ;method:" + method);
return;
}
try {
//
Object key;
List listTmp;
for (V val : list) {
key = method.invoke(val);
listTmp = map.get(key);
if (null == listTmp) {
listTmp = new ArrayList();
map.put((K) key, listTmp);
}
listTmp.add(val);
}
} catch (Exception e) {
System.out.print(" !");
}
}
}
2. import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.framework.common.CommonUtils.GroupBy;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//
final int loop = 1000 * 1000;
List list = new ArrayList(); // size=8 * loop
for (int i = 0; i < loop; i++) {
list.add(new Data().setId(1L).setCourseId(200010L).setContent("AAA"));
list.add(new Data().setId(2L).setCourseId(200010L).setContent("BBB"));
list.add(new Data().setId(3L).setCourseId(200011L).setContent("CCC"));
list.add(new Data().setId(4L).setCourseId(200011L).setContent("DDD"));
list.add(new Data().setId(5L).setCourseId(200010L).setContent("EEE"));
list.add(new Data().setId(6L).setCourseId(200011L).setContent("FFF"));
list.add(new Data().setId(7L).setCourseId(200010L).setContent("GGG"));
list.add(new Data().setId(8L).setCourseId(200012L).setContent("HHH"));
}
// 1
long time = System.currentTimeMillis();
Map> map2 = new LinkedHashMap>();
CommonUtils.listGroup2Map(list, map2, Data.class, "getId");//
long duration = System.currentTimeMillis() - time;
System.out.println(" :" + duration + " !");
//
time = System.currentTimeMillis();
Map> map = CommonUtils.group(list, new GroupBy() {
@Override
public Long groupby(Object obj) {
Data d = (Data) obj;
return d.getCourseId(); // ID
}
});
duration = System.currentTimeMillis() - time;
System.out.println(" :" + duration + " !");
}
}