写一个员工类Employee,有员工编号,姓名,年龄。 用map存储公司的员工,员工作为key,薪水作为value

要求:
输出所有员工的薪水 
员工离职
员工加薪

按员工年龄从大到小输出

public class Employee implements Comparable<Employee>{
	private String no;
	private String name;
	private int age;
	
	public Employee() {
		super();
	}
	
	public Employee(String no, String name, int age) {
		super();
		this.no = no;
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "编号:" + no + ", 姓名:" + name + ", 年龄:" + age;
	}

	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int compareTo(Employee o) {
		// TODO Auto-generated method stub
		return this.age - o.age;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((no == null) ? 0 : no.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (no == null) {
			if (other.no != null)
				return false;
		} else if (!no.equals(other.no))
			return false;
		return true;
	}

}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<Employee,Integer> map = new HashMap<Employee, Integer>();
		Employee em = new Employee();
		map.put(new Employee("A001", "张员工", 19), 3800);
		map.put(new Employee("A002", "赵员工", 23), 3500);
		map.put(new Employee("A005", "容员工", 21), 4000);
		map.put(new Employee("A003", "李员工", 20), 4600);
		map.put(new Employee("A004", "翟员工", 22), 3200);
		
		System.out.println("***遍历员工信息***");
		Set<Employee> set = map.keySet();
		for(Employee key : set) {
			System.out.println(key+ ", 工资 : " + map.get(key));
		}
		
		System.out.println("***员工加薪***");
		//所有员工加薪
		/*
		for(Employee key : set) {
			System.out.println(key+ ", 工资 : " + map.get(key)*2);
		}*/
		map.put(new Employee("A004", "翟员工", 22), 3800);
		for(Employee key : set) {
			System.out.println(key+ ", 工资 : " + map.get(key));
		}
		
		System.out.println("***按员工年龄从大到小排序***");
		//先从Map转化为list
		List<Map.Entry<Employee, Integer>> list = new ArrayList<Map.Entry<Employee,Integer>>(map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<Employee, Integer>>() {
			public int compare(Map.Entry<Employee, Integer> o1,
								Map.Entry<Employee, Integer> o2) {  
		        return ( o1.getKey().getAge()-o2.getKey().getAge());  

		    }
		});
		for (int i = 0; i < list.size(); i++) {  

		    Entry<Employee, Integer> ent=list.get(i);  

		    System.out.println(ent.getKey()+", 工资 : "+ent.getValue());
		}
		
		System.out.println("***员工离职***");
		
		map.remove(new Employee("A004", "翟员工", 22));
//		System.out.println(map);
		for(Employee key : set) {
			System.out.println(key+ ", 工资 : " + map.get(key));
		}
		
	}
	

}


  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值