Student 类
public class Student {
private String name;
private int age;
private int taller;
public Student( String name, int age, int taller ) {
this.name = name;
this.age = age;
this.taller = taller;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student [name=").append(name).append(", age=").append(age).append(", taller=").append(taller)
.append("]");
return builder.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setTaller( int taller ) {
this.taller = taller;
}
public int getTaller() {
return taller;
}
}
测试类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import PubClass.Student;
public class ListAddMap {
public static void main( String args[] ) {
List> list = new ArrayList>();
Map map = new HashMap();
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 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 mapList = list.get(i);
Iterator it = mapList.keySet().iterator();
while( it.hasNext() ) {
String str = (String) it.next();
System.out.println( "key-->" + str + "\t value-->" + map.get(str) );
}
}
}
}
运行结果