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

在这里插入图片描述

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的情况考虑到,成功解决

猜你喜欢

转载自blog.csdn.net/qq_45895520/article/details/120224970