emp002

import java.util.HashMap;

import java.util.Iterator;

import java.util.ListIterator;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

public class Welcome {

public static void main(String[] args) {

Map<String, String> map = new HashMap();

Emp emp = new Emp("001", "张三");

Emp emp2 = new Emp("002", "李四");

map.put(emp.getE_id(), emp.getE_name());

map.put(emp2.getE_id(), emp2.getE_name());

// 第一种:通过Map.keySet遍历key和value

System.out.println("通过Map.keySet遍历key和value:");

for (String key : map.keySet()) {

System.out.println("key= " + key + " and value= " + map.get(key));

}

// 第二种:通过Map.entrySet使用iterator遍历key和value

System.out.println("通过Map.entrySet使用iterator遍历key和value:");

Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();

while (it.hasNext()) {

Map.Entry<String, String> entry = it.next();

System.out.println("key= " + entry.getKey() + " and value= "

+ entry.getValue());

}

// 第三种:通过Map.entrySet遍历key和value

System.out.println("通过Map.entrySet遍历key和value:");

for (Map.Entry<String, String> entry : map.entrySet()) {

System.out.println("key= " + entry.getKey() + " and value= "

+ entry.getValue());

}

// 第四种:通过Map.values()遍历所有的value,但是不能遍历键key

System.out.println("通过Map.values()遍历所有的value:");

for (String v : map.values()) {

System.out.println("value= " + v);

}

}

}

class Emp {

private String e_id;

private String e_name;

public Emp(String e_id, String e_name) {

this.e_id = e_id;

this.e_name = e_name;

}

public String getE_id() {

return e_id;

}

public void setE_id(String eId) {

e_id = eId;

}

public String getE_name() {

return e_name;

}

public void setE_name(String eName) {

e_name = eName;

}

}