处理map集合
map集合的某个键值对一样时,将存在这个键值对的数据放到一起,组合成新的map集合。
要在页面实现某种布局,而将数据处理成某种格式。
代码:
public List<Map<String, Object>> getData(List<Map<String, Object>> param) {
//用来存放最终的数据
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
//用来存在具有某个相同的键值对的集合
Map<String, Map<String, Object>> cacheMap = new HashMap<String, Map<String, Object>>();
Map<String, Object> result1;
if (param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
Map<String, Object> shareMap = (Map<String, Object>) param.get(i);
String days = String.valueOf(shareMap.get("days"));//相同的键值对
result1 = cacheMap.get(days);
List<Map<String, Object>> dayList = null;
if (result1 == null) {
result1 = new HashMap<String, Object>();
dayList = new ArrayList<Map<String, Object>>();
result1.put("day", days);
result1.put("list", dayList);
result.add(result1);
cacheMap.put(days, result1);
} else {
dayList = (List<Map<String, Object>>) result1.get("list");
}
dayList.add(shareMap);
}
}
return result;
}
结果:
[{list=[
{storeName=蕾幔窗帘1店, days=574, storeAddress=蕾幔窗帘, storeLocation=null, storePhone=5454, brandName=蕾幔窗帘, brandLogo=/images/brandLogo/165283f417700ai.PNG}],
day=574},
{list=[
{storeName=隆万石材大五金店, days=573, storeAddress=大五金超市一楼, storeLocation=null, storePhone=13021978537, brandName=隆万石材, brandLogo=/
images/brandLogo/165318fc1ce00cs.PNG},
{storeName=隆万石材美家店, days=573, storeAddress=美家商城一楼, storeLocation=null, storePhone=0202-25812188, brandName=隆万石材, brandLogo=/images/brandLogo/165318fc1ce00cs.PNG},
{storeName=隆万石材滨海红星店, days=573, storeAddress=红星美凯龙一楼, storeLocation=null, storePhone=0022-66352368, brandName=隆万石材, brandLogo=/ima
ges/brandLogo/165318fc1ce00cs.PNG}], day=573}, {list=[{storeName=煜家灯具, days=572, storeAddress=红星美凯龙负一层G8809灯网, storeLocation=nu
ll, storePhone=0202-84157645, brandName=煜家灯具, brandLogo=/images/brandLogo/16528315f8d00ac.PNG},
{storeName=煜家灯具建材路环渤海店, days=572, storeAddress=环渤海负一层F8013灯网, storeLocation=null, storePhone=0202-60121171, brandName=煜家灯具, brandLogo
=/images/brandLogo/16528315f8d00ac.PNG}], day=572},
{list=[{storeName=A, days=571, storeAddress=A, storeLocation=null, storePhone=10312056578,
brandName=A, brandLogo=/images/brandLogo/165281cceee00ab.PNG}], day=571}, {list=[{storeName=布艺建材路环渤海店, days=570, storeAddress= 建材路
环渤海国际家居三层C8308, storeLocation=null, storePhone=0202-60123870, brandName=宏昇家居布艺, brandLogo=/images/brandLogo/16527899b7700aa.PNG}], day=570}]
结束语:
本人小菜鸡一枚,希望这段代码能够对别人有所帮助。同时,希望大家多提建议,不吝赐教。