import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author EVA LEE
* @version 1.0
* @time 2023/12/30 22:32
*/
@SuppressWarnings({"all"})
public class Ex01 {
public static void main(String[] args) {
/*
使用HashMap 添加3个员工对象
要求:
key:员工id
value:员工对象
并遍历显示工资>18000 的员工(遍历方式最少两种)
员工类: name sal ID
*/
Map hashMap = new HashMap();
hashMap.put(1, new Employee("1", 11, 10000));
hashMap.put(2, new Employee("2", 22, 20000));
hashMap.put(3, new Employee("3", 33, 30000));
Set keySet = hashMap.keySet();
// keyset:
for (Object key : keySet) {
Employee e = (Employee) hashMap.get(key);
if (e.getSal() > 10000) {
System.out.println(e);
}
}
Iterator iterator2 = keySet.iterator();
while (iterator2.hasNext()) {
Object next = iterator2.next();
Employee e2 = (Employee) hashMap.get(next);
if (e2.getSal() > 10000) {
System.out.println(e2);
}
}
// entryset:
Set set = hashMap.entrySet();
Iterator iterator1 = set.iterator();
while (iterator1.hasNext()) {
Object next = iterator1.next();
Map.Entry m = (Map.Entry) next;
//Employee e3 = (Employee) hashMap.get(m);
// System.out.println(m.getValue().getClass());
Employee e4 = (Employee) m.getValue();
if (e4.getSal() > 10000) {
System.out.println(e4);
}
}
for (Object object : set) {
Map.Entry entry = (Map.Entry) object;
Employee b = (Employee) entry.getValue();
if (b.getSal() > 10000) {
System.out.println(b);
}
}
}
}
@SuppressWarnings({"all"})
class Employee {
private String name;
private int ID;
private double sal;
public Employee(String name, int ID, double sal) {
this.name = name;
this.ID = ID;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", ID=" + ID +
", sal=" + sal +
'}';
}
}
Map-HSP- Exercise(k-v)
于 2023-12-31 12:13:55 首次发布