遍历list集合泛型为map
public class ListAddMap {
public static void main( String args[] ) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Student stu1 = new Student( "\u5f20\u4e09", 26, 180 );
Student stu2 = new Student( "\u674e\u56db", 28, 175 );
map.put( "stu1", stu1 );
map.put( "stu2", stu2 );
list.add( map );
System.out.println( "方法一 :" );
for( int i = 0; i < list.size(); i++ ) {
System.out.println( list.get(i) );
}
System.out.println( "方法二 :" );
for( Map<String, Object> mapList : list ) {
for( String key : mapList.keySet() ) {
System.out.println( key + "-->" + mapList.get(key) );
}
}
System.out.println( "方法三 :" );
for( int i = 0; i < list.size(); i++ ) {
Map<String, Object> mapList = list.get(i);
Iterator<String> it = mapList.keySet().iterator();
while( it.hasNext() ) {
String str = (String) it.next();
System.out.println( "key-->" + str + "\t value-->" + map.get(str) );
}
}
}
}
运行结果
---------------------
作者:柒弦宇轩
来源:CSDN
原文:https://blog.csdn.net/qq_35958249/article/details/85231798
版权声明:本文为博主原创文章,转载请附上博文链接!