利用集合存入对象并排序

问题描述:
  要求用户首先输入员工数量,然后输入相应员工信息,格式为:
* name,age,gender,salary,hiredate* 例如:
* 张三,25,男,5000,2006-02-15
* 每一行为一个员工信息,然后将每个员工信息解析成Emp对象。并存入到一个集合中。
* 在解析成Emp对象后要先查看当前集合是否包含该员工,若包含则提示该用于已存在,否则才存入集合。
* 然后输出集合查看每个员工信息.
* 最后根据员工入职日期排序,最晚的在最前面.
package package4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
class Employee implements Comparator<Employee>{
	private String name;
	private int age;
	private String gender;
	private int salary;
	private Date hireDate;
	
	public Employee(String name, int age, String gender, int salary,
			Date hireDate) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.salary = salary;
		this.hireDate = hireDate;
	}
	public Employee() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", gender=" + gender
				+ ", salary=" + salary + ", hireDate=" + hireDate + "]";
	}
	
	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;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public Date getHireDate() {
		return hireDate;
	}
	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}
	@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 (gender == null) {
			if (other.gender != null)
				return false;
		} else if (!gender.equals(other.gender))
			return false;
		if (hireDate == null) {
			if (other.hireDate != null)
				return false;
		} else if (!hireDate.equals(other.hireDate))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (salary != other.salary)
			return false;
		return true;
	}
	@Override
	public int compare(Employee arg0, Employee arg1) {
		// TODO Auto-generated method stub
		return arg0.getHireDate().getTime()-
				arg1.getHireDate().getTime()>0? -1:1;
	}
}
public class EmployeeDemo {
	public static void main(String[] args) {
		System.out.println("how many employee:");
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		Employee[] ee = new Employee[num];
		System.out.println("enter the info of employee:");
		System.out.println("format:name,age,gender,salary,hireDate");
		Scanner sc = new Scanner(System.in);
		for(int i = 0;i < num;i++){
			System.out.println("enter the "+(i+1)+"th Employee's info:");
			String str = sc.nextLine();
			String[] strs = str.split(",");	
			String name = strs[0];
			int age = Integer.parseInt(strs[1]);
			String gender = strs[2];
			int salary = Integer.parseInt(strs[3]);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date hireDate = sdf.parse(strs[4]);
				ee[i] = new Employee(name,age,gender,salary,hireDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}}
		Collection<Employee> coll = new ArrayList<Employee>();
		for(int i=0;i<num;i++) {
			if(!coll.contains(ee[i])) {
				coll.add(ee[i]);
			}}
		System.out.println("after traverse the collection:");
		for(Employee e : coll) {
			System.out.println(e);}	
		Collections.sort((List)coll,new Employee());
		System.out.println("after sort with hireDate:");
		Iterator<Employee> it = coll.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}	
	}
}

控制台输出结果为:
how many employee:
4
enter the info of employee:
format:name,age,gender,salary,hireDate
enter the 1th Employee's info:
aaa,20,male,4000,2016-1-10
enter the 2th Employee's info:
bbb,21,female,5000,2017-4-6
enter the 3th Employee's info:
ccc,23,male,3500,2015-10-10
enter the 4th Employee's info:
ccc,23,male,3500,2015-10-10
after traverse the collection:
Employee [name=aaa, age=20, gender=male, salary=4000, hireDate=Sun Jan 10 00:00:00 CST 2016]
Employee [name=bbb, age=21, gender=female, salary=5000, hireDate=Thu Apr 06 00:00:00 CST 2017]
Employee [name=ccc, age=23, gender=male, salary=3500, hireDate=Sat Oct 10 00:00:00 CST 2015]
after sort with hireDate:
Employee [name=bbb, age=21, gender=female, salary=5000, hireDate=Thu Apr 06 00:00:00 CST 2017]
Employee [name=aaa, age=20, gender=male, salary=4000, hireDate=Sun Jan 10 00:00:00 CST 2016]
Employee [name=ccc, age=23, gender=male, salary=3500, hireDate=Sat Oct 10 00:00:00 CST 2015]

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值