java(Collection接口)练习07

本文介绍了一个Java程序案例,定义了Employee类,并通过List和Set集合来存储及操作Employee对象。首先创建了若干Employee实例,接着展示了如何在List中对这些对象进行排序:按工资从高到低,工资相同按年龄从大到小,年龄也相同则按姓名升序排列。最后将Employee对象存入Set集合中,并确保没有重复元素。
摘要由CSDN通过智能技术生成

.定义一个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);
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值