创建map集合,创建Emp对象001,张三;002,李四,遍历集合
1个回答
2013-11-10
展开全部
import java.util.*;
import java.util.Map.Entry;
public class HelloWorld {
public static void main(String[] args) {
Map<Integer, Stu> stuMap = new HashMap<Integer, Stu>();
stuMap.put(1, new Stu("张三"));
stuMap.put(2, new Stu("李四"));
Iterator<Entry<Integer, Stu>> it = stuMap.entrySet().iterator();
while (it.hasNext())
System.out.println(it.next());
// or
Set<Integer> set = stuMap.keySet();
for (Integer integer : set) {
System.out.println(integer + ", " + stuMap.get(integer).getName());
}
}
}
class Stu {
@Override
public String toString() {
return this.name;
}
private String name;
public Stu() {
}
public Stu(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.Map.Entry;
public class HelloWorld {
public static void main(String[] args) {
Map<Integer, Stu> stuMap = new HashMap<Integer, Stu>();
stuMap.put(1, new Stu("张三"));
stuMap.put(2, new Stu("李四"));
Iterator<Entry<Integer, Stu>> it = stuMap.entrySet().iterator();
while (it.hasNext())
System.out.println(it.next());
// or
Set<Integer> set = stuMap.keySet();
for (Integer integer : set) {
System.out.println(integer + ", " + stuMap.get(integer).getName());
}
}
}
class Stu {
@Override
public String toString() {
return this.name;
}
private String name;
public Stu() {
}
public Stu(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询