定义一个Employee类,属性:name:String,age:int,salary:double 把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面

定义一个Employee类,属性:name:String,age:int,salary:double 把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面_第1张图片

package cn.kgc.lx02;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*定义一个Employee类,属性:name:String,age:int,salary:double 
把若干Employee对象放在List中,排序并遍历输出,
排序规则:salary高的在前面,salary相同时age大的在前面,age也相同时按照name升序排列
把若干Employee对象放在Set中并遍历,要求没有重复元素*/
public class Lx2 {
	public static void main(String[] args) {
		Employee em=new Employee();
		List list=new ArrayList();
		list.add(new Employee("z张三", 20, 4000));
		list.add(new Employee("l李四", 35, 3000));
		list.add(new Employee("w王二", 20, 30000));
		list.add(new Employee("x小明", 30, 30000));
		list.add(new Employee("a小兰", 30, 30000));
		
		for (Employee employee : list) {
			System.out.println(employee);
		}
		System.out.println("=================================");
		em.px(list);//调用排序方法
		for (Employee employee : list) {
			System.out.println(employee);
		}
		System.out.println("=================================");
		
		Set set1=new HashSet();
		Set set2=new HashSet();
		Set set3=new HashSet();
		set1.addAll(list);//把若干Employee对象放在Set中
		for (Employee employee : set1) {
			boolean b1=set2.add(employee.getName());
			boolean b2=set2.add(employee.getAge());
			boolean b3=set2.add(employee.getSalary());
			if(!b1||!b2||!b3) {
				set3.add(employee);//找出有重复元素的对象
			}
		}
		set1.removeAll(set3);//移除重复元素的对象
		for (Employee employee : set1) {
			System.out.println(employee);
		}
	}
}

Employee类:

package cn.kgc.lx02;

import java.util.List;

/*定义一个Employee类,属性:name:String,age:int,salary:double */
public class Employee {
	private String name;
	private int age;
	private double salary;
	public Employee() {
		super();
	}
	public Employee(String name, int age, double salary) {
		super();
		this.name = name;
		this.age = age;
		this.salary = salary;
	}
	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 double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "name=" + name + ", age=" + age + ", salary=" + salary ;
	}
	public List px(List list){
		for(int i=0;i0) {
							list.set(i, s2);
							list.set(j, s1);
						}
					}
				}
			}
		}
		return list;
	}
}

你可能感兴趣的:(练习)