Map接口练习题

题目:使 用 HashMap 添 加 3 个 员 工 对 象 , 要 求 键 : 员 工 id 值 : 员 工 对 并 遍 历 显 示 工 资 > 18000 的 员 工 ( 遍 历 方 式 最 少 两 种 ) 员 工 类 : 姓 名 、 工 资 、 员 工 id

 public static void main(String[] args) {
​
        Map map = new HashMap();
        map.put(1,new Worker("tom",20000,10010));
        map.put(2,new Worker("jason",19000,10011));
        map.put(3,new Worker("mike",17000,10012));
​
        //1.遍历
        Set set = map.keySet();
        for (Object key : set) {
            Worker worker = (Worker) map.get(key);
            if(worker.getSal() > 18000)
            System.out.println(key + ":" + map.get(key));
            //1:tom 20000 10010  2:jason 19000  10011
        }
​
​
        System.out.println("===================================");
​
        //2.遍历
        Set entrySet = map.entrySet();
        for (Object entry : entrySet) {
           Map.Entry m = (Map.Entry) entry;
            Worker worker = (Worker) m.getValue();
            if(worker.getSal() > 18000)
                System.out.println(m.getKey() + ":" + m.getValue());
                //1:tom 20000 10010  2:jason 19000  10011
        }
    }
​
}
​
class Worker{
    private String name;
    private double sal;
    private int id;
​
    public Worker(String name, double sal, int id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public double getSal() {
        return sal;
    }
​
    public void setSal(double sal) {
        this.sal = sal;
    }
​
    public int getId() {
        return id;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    @Override
    public String toString() {
        return name + "\t" + sal  + "\t" + id ;
​
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhan;

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值