【Java List与Map】List<T> Map与Map List<T>的区别(126)

List<T> Map:List里面的数据类型包含Map;
Map List<T>:Map里面value的数据类型包含List;

测试案例

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class day20 {
	public static void main(String[] args) {
		
		Map<String,Object> map1 = new HashMap<>();
		map1.put("name","小婷");
		map1.put("age",20);
		Map<String,Object> map2 = new HashMap<>();
		map2.put("name","小霞");
		map2.put("age",22);
		Map<String,Object> map3 = new HashMap<>();
		map3.put("name","小桐");
		map3.put("age",24);
		
		// Map集合:List<Map<String,Object>>:
		List<Map<String,Object>> list = new ArrayList<>();
		list.add(map1);
		list.add(map2);
		list.add(map3);
		for(Map<String,Object> map :list) {
			System.out.println("List<T> Map:"+map);
		}
		System.out.println("---------------------");
		
		// 集合Map:Map<String,Object>:
		Map<String,Object> map = new HashMap<>();
		
		List<Map<String,Object>> list2 = new ArrayList<>();
		Map<String,Object> map4 = new HashMap<>();
		map4.put("name","小英");
		map4.put("age",20);
		Map<String,Object> map5 = new HashMap<>();
		map5.put("name","小红");
		map5.put("age",22);
		Map<String,Object> map6 = new HashMap<>();
		map6.put("name","小婵");
		map6.put("age",24);
		list2.add(map4);
		list2.add(map5);
		list2.add(map6);
		
		map.put("list1", list);
		map.put("list2", list2);
		System.out.println("Map List<T>1:"+map);
		System.out.println("---------------------");
		
		Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Object> entry = it.next();
            System.out.println("Map List<T>2:"+"key="+entry.getKey()+"  value="+entry.getValue());
        }
		
	}
}

测试输出

ListTMap:{name=小婷, age=20}
ListTMap:{name=小霞, age=22}
ListTMap:{name=小桐, age=24}
---------------------
Map ListT1:{list1=[{name=小婷, age=20}, {name=小霞, age=22}, {name=小桐, age=24}], list2=[{name=小英, age=20}, {name=小红, age=22}, {name=小婵, age=24}]}
---------------------
Map ListT2:key=list1  value=[{name=小婷, age=20}, {name=小霞, age=22}, {name=小桐, age=24}]
Map ListT2:key=list2  value=[{name=小英, age=20}, {name=小红, age=22}, {name=小婵, age=24}]