HashMap中的内容进行迭代输出

package demo20130418;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<Integer,User> map=new HashMap<Integer,User>();
		User user=new User(1,"ctl","qq.com",'m');
		map.put(user.getId(), user);
		user=new User(2,"lr","qq.com",'w');
		map.put(user.getId(), user);
		System.out.println(map);
		Set<Entry<Integer,User>> set=map.entrySet();
		Iterator it=set.iterator();
		for(;it.hasNext();){
			System.out.println(it.next());
		}
				//new HashSet<Entry<Integer,User>>();
		//Iterator<Integer,User> iterator=map
	}

}
class User{
	private int id;
	private String name;
	private String email;
	private char sex;
	public User(int id, String name, String email, char sex) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public String toString(){
		return id+" "+name+" "+email+" "+sex;
	}
}


posted @ 2013-04-18 21:39  xinyuyuanm  阅读(327)  评论(0编辑  收藏  举报