java Object类是可以接收集合类型的
废话不多说,上代码:
package com.core.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ObjectListTest { public static void main(String[] args) { Object obj = createMap(); if(obj instanceof Map){ Map<String,Object> map =(Map)obj; System.out.println(map.get("name")); } Object obj1 =createList(); if(obj1 instanceof List){ List<String> list = (List)obj1; for(int i = 0 ; i < list.size() ; i++ ){ System.out.println(list.get(i)); } } } public static Map<String,Object> createMap(){ Map<String, Object> map =new HashMap<String, Object>(); map.put("name", "张飞"); map.put("age","10"); return map; } public static List<String> createList(){ List<String> list =new ArrayList<String>(); for(int i = 0 ; i < 10 ; i++){ list.add("吕布"+i); } return list; } }
结果:
张飞
吕布0
吕布1
吕布2
吕布3
吕布4
吕布5
吕布6
吕布7
吕布8
吕布9