.定义一个Employee类,属性:name:String,age:int,salary:double
把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面,salary相同时age大的在前面,age也相同时按照name升序排列
把若干Employee对象放在Set中并遍历,要求没有重复元素
package com.oracle.test1025;
public class Employee {
private String name;
private int age;
private double 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;
}
public Employee(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "姓名:" + name + ", 年龄:" + age + ", 工资:" + salary ;
}
}
=================================================================
package com.oracle.test1025;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("大海燕",19,3000);
Employee e2 = new Employee("小黑",20,3100);
Employee e3 = new Employee("傲娇",18,4000);
Employee e4 = new Employee("苏老大",22,5000);
Employee e5 = new Employee("马冬梅",21,2000);
Employee e6 = new Employee("女神",18,7000);
List<Employee> list = new ArrayList<Employee>();
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
list.add(e5);
list.add(e6);
for (Employee e : list) {
System.out.println(e);
}
System.out.println("==============排序后==================");
for (int i = 1; i < list.size(); i++) {
for (int j = 0; j < list.size()-i; j++) {
Employee emp1 = list.get(j);
Employee emp2 = list.get(j+1);
//比较收入
if(emp1.getSalary()<emp2.getSalary()){
list.set(j, emp2);
list.set(j+1, emp1);
}else if(emp1.getSalary() == emp2.getSalary()){
//比较年龄
if(emp1.getAge() < emp2.getAge()){
list.set(j, emp2);
list.set(j+1, emp1);
}else if(emp1.getAge() == emp2.getAge()){
if(emp1.getName().compareTo(emp2.getName())>0){
list.set(j, emp2);
list.set(j+1, emp1);
}
}
}
}
}
for (Employee e : list) {
System.out.println(e);
}
}
}