java list集合分组_java中对list集合中的数据按照某一个属性进行分组

importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Set;public classListSort {/*** 按照List>里面map的某个value重新封装成多个不同的list, 原始数据类型List

* >, 转换后数据类型Map>>

*

*@paramlist

*@paramoneMapKey

*@return*/private static Map change(List>list, String oneMapKey) {

Map resultMap = new HashMap();

Set setTmp = new HashSet();for (Maptmp : list) {

setTmp.add(tmp.get(oneMapKey));

}

Iterator it =setTmp.iterator();while(it.hasNext()) {

String oneSetTmpStr=(String) it.next();

List> oneSetTmpList = new ArrayList>();for (Maptmp : list) {

String oneMapValueStr=(String) tmp.get(oneMapKey);if(oneMapValueStr.equals(oneSetTmpStr)) {

oneSetTmpList.add(tmp);

}

}

resultMap.put(oneSetTmpStr, oneSetTmpList);

}returnresultMap;

}/*** 按照List>里面map的某个value重新封装成多个不同的list, 原始数据类型List

* >, 转换后数据类型Map>>

*

*@paramlist

*@paramoneMapKey

*@return*/private static List> change2(List>inList, String oneMapKey,

List>outList) {//1.将某个key的值存在set中

Set setTmp = new HashSet();for (Maptmp : inList) {

setTmp.add(tmp.get(oneMapKey));

}//2.遍历set

Iterator it =setTmp.iterator();while(it.hasNext()) {

String oneMapValueStr= "";

String oneSetTmpStr=(String) it.next();

Map oneSetTmpMap = new HashMap();

List> oneSetTmpList = new ArrayList>();for (Maptmp : inList) {

oneMapValueStr=(String) tmp.get(oneMapKey);if(oneSetTmpStr.equals(oneMapValueStr)) {

oneSetTmpMap.put("text", oneSetTmpStr);

oneSetTmpList.add(tmp);

}

}

oneSetTmpMap.put("array", oneSetTmpList);

outList.add(oneSetTmpMap);

}returnoutList;

}public static voidmain(String[] args) {

Map map1 = new HashMap();

Map map2 = new HashMap();

Map map3 = new HashMap();

Map map4 = new HashMap();

List> inList = new ArrayList>();

map1.put("id", "111");

map1.put("name", "tom");

map1.put("sex", "male");

map2.put("id", "222");

map2.put("name", "jack");

map2.put("sex", "male");

map3.put("id", "333");

map3.put("name", "lucy");

map3.put("sex", "female");

map4.put("id", "444");

map4.put("name", "lucy");

map4.put("sex", "female");

inList.add(map1);

inList.add(map2);

inList.add(map3);

inList.add(map4);

System.out.println("原始inList:" +inList);

Map resultMap = new HashMap();//resultMap = change(list, "sex");

resultMap = change(inList, "name");

System.out.println("按照map中的某个值重新封装后的resultMap:" +resultMap);

List> outList = new ArrayList>();

System.out.println("retList==1==:" +outList);

outList= change2(inList, "name", outList);

System.out.println("retList==2==:" +outList);

}

}

————————————————

版权声明:本文为CSDN博主「FOCUS_ON_AI」的原创文章,遵循 CC4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_32649581/article/details/82851701

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值