获取Map中的List值

List<Object> list =  new ArrayList<Object>();

Student s = new Student();
s.setAddress("北京");
s.setAge(10);
s.setName("keney1");
list.add(s);

Student s2 = new Student();
s2.setAddress("上海");
s2.setAge(11);
s2.setName("keney2");
list.add(s2);
        Map<Object,Object> map = new HashMap<Object,Object>();
map.put("stu1", list);
map.put("stu2", list);







  /***
   * 根据MAP中的key值取得对应的value
   */

  List<Student> testList = (List<Student>) map.get("stu1");
for (Student test : testList) { 
System.out.println( test.getName() + test.getAddress()); 



/**
* 遍历MAP种的List得出List对象属性值
*/

Set<Object> keySet = map.keySet();
for(Iterator<Object> i=keySet.iterator();i.hasNext();){
Object key = i.next();
System.out.println(key);

List<Student> testList1 =  (List<Student>)map.get(key); 
for (Student test : testList) { 
System.out.println( test.getName() + test.getAddress()); 

}



你可能感兴趣的:(java 编程)