Map练习

package com.collection_.map_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@SuppressWarnings({"all"})
public class MapExercise {
    public static void main(String[] args) {
        /*
        使用HashMap添加3个员工对象,要求
            键:员工id
            值:员工对象
            并遍历显示工资>18000的员工(遍历方式最少两种)
            员工类:姓名、工资、员工id
         */
        Map hashMap = new HashMap();
        hashMap.put(1,new Employee("张三",19000,1));
        hashMap.put(2,new Employee("李四",16000,2));
        hashMap.put(3,new Employee("王五",30000,3));
        //1. 使用keySet来遍历
        Set keySet = hashMap.keySet();
        //增强for
        System.out.println("========keySet 增强for遍历");
        for (Object key : keySet) {
            //取出value(转换成 employee对象才可以调用Salary)
            Employee emp = (Employee) hashMap.get(key);
            if (emp.getSalary()>18000) {
                System.out.println(key +"-" + hashMap.get(key));
            }
        }
        //迭代器
        System.out.println("========keySet 迭代器遍历");
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            Employee emp = (Employee) hashMap.get(key);
            if (emp.getSalary()>18000) {
                System.out.println(key +"-" + hashMap.get(key));
            }
        }
        //2. 通过EntrySet遍历
        Set entrySet = hashMap.entrySet();
        //增强for
        System.out.println("========Entry 增强for遍历");
        for (Object entry :entrySet) {
            //转换成Map.Entry 调用getKey() 和getValue()
            Map.Entry m = (Map.Entry) entry;
            //取出value
            Employee emp = (Employee) m.getValue();
            if (emp.getSalary()>18000) {
                System.out.println(m.getKey() +"-" + m.getValue());
            }
        }
        //迭代器
        System.out.println("========EntrySet 迭代器遍历");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()){
            Object entry = iterator2.next();//Map.Entry entry = (Map.Entry) iterator2.next();
            //转换成Map.Entry 调用getKey() 和getValue()
            Map.Entry m = (Map.Entry) entry;
            //取出value
            Employee emp = (Employee) m.getValue();
            if (emp.getSalary()>18000) {
                System.out.println(m.getKey() +"-" + m.getValue());
            }
        }
    }

}
//员工类:姓名、工资、员工id
class Employee{
    private String name;
    private double salary;
    private int id;

    public Employee(String name, double salary, int id) {
        this.name = name;
        this.salary = salary;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", id=" + id +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值