package com.wyz.test;
import java.util.*;
public class test{
public static void main(String[] args) {
Map map = new HashMap();
Employee e1 = new Employee("001", "wyz", 20000);
Employee e2 = new Employee("002", "wzp", 10000);
Employee e3 = new Employee("003", "chw", 30000);
map.put(e1.getId(), e1);
map.put(e2.getId(), e2);
map.put(e3.getId(), e3);
Set keyset = map.keySet();
System.out.println("--------第一种遍历方法(1)---------");
for (Object key : keyset) {
System.out.println(key + "代表的对象是:" + map.get(key));
}
System.out.println("--------第一种遍历方法(2)---------");
Iterator iterator = keyset.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key + "代表的对象是:" + map.get(key));
}
Collection values = map.values();
System.out.println("--------第二种遍历方法(1)---------");
for (Object value : values) {
System.out.println("对象的值是:" + value);
}
System.out.println("--------第二种遍历方法(2)---------");
iterator = values.iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
System.out.println("对象的值是:" + value);
}
System.out.println("--------第三种遍历方法(1)---------");
Set entrySet = map.entrySet();
for (Object entry : entrySet) {
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "代表的对象是:" + m.getValue());
}
System.out.println("--------第三种遍历方法(2)---------");
iterator = entrySet.iterator();
while (iterator.hasNext()) {
Object entry = iterator.next();
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "代表的对象是:" + m.getValue());
}
}
}
class Employee{
private String id;
private String name;
private int salary;
public Employee(String id, String name, int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
12-04
186
07-17
140
03-13
3651