Java中List<T> Map与Map List<T>的区别小结
作者:DevínKelly
本文主要介绍了Java中List<T> Map与Map List<T>的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
- 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()); } } }
测试输出:
List<T> Map:{name=小婷, age=20}
List<T> Map:{name=小霞, age=22}
List<T> Map:{name=小桐, age=24}
---------------------
Map List<T>1:{list1=[{name=小婷, age=20}, {name=小霞, age=22}, {name=小桐, age=24}], list2=[{name=小英, age=20}, {name=小红, age=22}, {name=小婵, age=24}]}
---------------------
Map List<T>2:key=list1 value=[{name=小婷, age=20}, {name=小霞, age=22}, {name=小桐, age=24}]
Map List<T>2:key=list2 value=[{name=小英, age=20}, {name=小红, age=22}, {name=小婵, age=24}]
到此这篇关于Java中List<T> Map与Map List<T>的区别小结的文章就介绍到这了,更多相关Java List<T> Map与Map List<T>内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!