使用Map对员工的编号工资排序(TreeMap练习)TreeMap遇到NullPointException

该代码示例展示了如何实现一个 Employee 类,包括姓名、薪水和 ID 的属性,并实现了 Comparable 接口进行比较。此外,还定义了一个 Add 类,使用 TreeMap 存储 Employee 对象并按 ID 和薪水排序。在 Test01 类中创建了 Employee 实例并调用 Add 类的方法进行操作,最后打印出排序后的员工信息。
摘要由CSDN通过智能技术生成

在这里插入图片描述

public class Employee implements Comparable{
	private String name;
	private Integer salary;
	private Integer id;
	public Employee() {
		super();
	}
	
	public Employee(String name, Integer id) {
		super();
		this.name = name;
		this.id = id;
	}

	public Employee(String name, Integer salary, Integer id) {
		super();
		this.name = name;
		this.salary = salary;
		this.id = id;
	}

	
	public String getName() {
		return name;
	}

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

	public Integer getSalary() {
		if (this.salary == null){
			salary = 0;
		}
		return salary;
	}

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

	public Integer getId() {
		return id;
	}

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

	@Override
	public String toString() {
		return "Employee [name=" + name + ", salary=" + salary + ", id=" + id + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.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 (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Employee e = (Employee) o;
		int one = this.getId().compareTo(e.getId());
		if (one == 0) {
			one = this.getSalary().compareTo(e.getSalary());
		}
		return one;
	}
	
}

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

public class Add {
	Map m1 = new TreeMap();
	public TreeMap add(Employee e, Integer salary) {
		int salary0 = salary;
		m1.put(e, salary0);
		e.setSalary(salary0);
		return (TreeMap) m1;
		
	}
	public void bianLi() {
		Set set = m1.keySet();
		Iterator iterator = set.iterator();
	    while (iterator.hasNext()) {
	    	Employee e1 = (Employee) iterator.next();
	    	System.out.println(e1.toString());
	    }
	}
}

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

public class Test01 {
	public static void main(String[] args) {
		Add a = new Add();
		a.add(new Employee("张三",01), 18000);
		a.add(new Employee("李四",00), 17000);
		a.add(new Employee("王二",02), 19000);
		a.bianLi();
	}
}

结果:
Employee [name=李四, salary=17000, id=0]
Employee [name=张三, salary=18000, id=1]
Employee [name=王二, salary=19000, id=2]

开始的时候

	public Integer getSalary() {
		return salary;
	}

直接这样写的代码,结果出现了NullPointException异常
后来,将会出现null的情况考虑到,成功解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值