List Map String,Object 轉換成Map String,List Map String,Object
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set;
public class PartnerServiceImplTest {
public static void main(String[] args) { Map<String, Object> map1 = new HashMap<String, Object>(); Map<String, Object> map2 = new HashMap<String, Object>(); Map<String, Object> map3 = new HashMap<String, Object>(); Map<String, Object> map4 = new HashMap<String, Object>();
List<Map<String, Object>> inList = new ArrayList<Map<String, Object>>(); 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<String, Object> resultMap = new HashMap<String, Object>(); // resultMap = change(list, "sex"); resultMap = change(inList, "name"); System.out.println("按照map中的某個值重新封裝後的resultMap:" + resultMap);
List<Map<String, Object>> outList = new ArrayList<Map<String, Object>>(); System.out.println("retList==1==:" + outList); outList = change2(inList, "name", outList); System.out.println("retList==2==:" + outList);
}
/** * 按照List<Map<String,Object>>裡面map的某個value重新封裝成多個不同的list, 原始資料型別List<Map * <String,Object>>, 轉換後資料型別Map<String,List<Map<String,Object>>> * * @param list * @param oneMapKey * @return */ private static Map<String, Object> change(List<Map<String, Object>> list, String oneMapKey) { Map<String, Object> resultMap = new HashMap<String, Object>(); Set<Object> setTmp = new HashSet<Object>(); for (Map<String, Object> tmp : list) { setTmp.add(tmp.get(oneMapKey)); } Iterator<Object> it = setTmp.iterator(); while (it.hasNext()) { String oneSetTmpStr = (String) it.next(); List<Map<String, Object>> oneSetTmpList = new ArrayList<Map<String, Object>>(); for (Map<String, Object> tmp : list) { String oneMapValueStr = (String) tmp.get(oneMapKey); if (oneMapValueStr.equals(oneSetTmpStr)) { oneSetTmpList.add(tmp); } } resultMap.put(oneSetTmpStr, oneSetTmpList); } return resultMap; }
/** * 按照List<Map<String,Object>>裡面map的某個value重新封裝成多個不同的list, 原始資料型別List<Map * <String,Object>>, 轉換後資料型別Map<String,List<Map<String,Object>>> * * @param list * @param oneMapKey * @return */ private static List<Map<String, Object>> change2(List<Map<String, Object>> inList, String oneMapKey, List<Map<String, Object>> outList) { // 1.將某個key的值存在set中 Set<Object> setTmp = new HashSet<Object>(); for (Map<String, Object> tmp : inList) { setTmp.add(tmp.get(oneMapKey)); } // 2.遍歷set Iterator<Object> it = setTmp.iterator(); while (it.hasNext()) { String oneMapValueStr = ""; String oneSetTmpStr = (String) it.next(); Map<String, Object> oneSetTmpMap = new HashMap<String, Object>(); List<Map<String, Object>> oneSetTmpList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> tmp : inList) { oneMapValueStr = (String) tmp.get(oneMapKey); if (oneSetTmpStr.equals(oneMapValueStr)) { oneSetTmpMap.put("text", oneSetTmpStr); oneSetTmpList.add(tmp); } } oneSetTmpMap.put("array", oneSetTmpList); outList.add(oneSetTmpMap); } return outList; } }