【问题标题】:Java object array sortJava对象数组排序
【发布时间】:2021-06-13 11:05:18
【问题描述】:

我想根据整数值workhour对下面代码块中的添加数组进行排序,但是我从mySQL获取信息,我需要将它显示在JTable中。如何排序?

public void monthOfEmployee() {
    model.setRowCount(0);
    ArrayList<Employee> employees = new ArrayList<Employee>();
    employees = process.showEmployee();

    if (employees != null) {
        for (Employee employee : employees) {
            Object[] adding = { employee.getID(), employee.getName(), employee.getSalary(),
                    employee.getWorkingHour() };

            model.addRow(adding);
        }
    }
}

SQL 查询

public ArrayList<Employee> showEmployee() {

    ArrayList<Employee> tried = new ArrayList<Employee>();
    try {
        statement = con.createStatement();
        String query = "Select * From employee";

        ResultSet rs = statement.executeQuery(query);

        while (rs.next()) {
            String iD = rs.getString("emp_ID");
            int workingHour = rs.getInt("working_hour");
            int salary = rs.getInt("salary");
            String name = rs.getString("emp_name");

            tried.add(new Employee(name, iD, salary, workingHour));

        }
        return tried;

    } catch (SQLException ex) {
        Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

【问题讨论】:

标签: java arrays sorting


【解决方案1】:

如果你可以避免使用 Object[],你可以使用这个方法,利用 Java 8 中提供的 Comparator 类

List<Employee> employees = Arrays.asList(
                new Employee("One", 13),
                new Employee("Two", 8),
                new Employee("Three", 2),
                new Employee("Four", 11)
        );

employees.sort(Comparator.comparing(Employee::getWorkingHour));

【讨论】:

    【解决方案2】:

    您的 Employee 类需要实现 Comparable 接口并提供 compareTo 方法的定义。您可以根据您选择的任何字段进行排序。在您的情况下,它的 workingHour 整数字段。

    以下内容可能会进一步帮助您。我采用了两种方法:一种使用默认排序的 TreeSet,另一种使用 Java 8 stream.sorted。

    public class Sortedtest
    {
        public static void main(String[] args) {
            /** Method 1: having a treeset which is sorted itself */
            TreeSet<Employee> employees = new TreeSet<>();
            Employee employee1 = new Employee(3, "Raj", 20,9);
            Employee employee2 = new Employee(5, "Shyam", 18,8);
            Employee employee3 = new Employee(1, "Ram", 19,9);
            Employee employee4 = new Employee(4, "Sunil", 25,8);
            Employee employee5 = new Employee(2, "Ajay", 26,7);
            employees.add(employee1);
            employees.add(employee2);
            employees.add(employee3);
            employees.add(employee4);
            employees.add(employee5);
    
            // sorted  by themselves
            System.out.println("Sorted using Treeset: " + employees);
    
    
            /** Method 2: using Stream.sorted method */
    
            List<Employee> employeeList = Arrays.asList(employee1, employee2, employee3, employee4, employee5);
            List<Employee> sortedList = employeeList.stream().sorted().collect(Collectors.toList());
            System.out.println("Sorted using Stream: " + sortedList);
        }
    }
    
    
    class Employee implements Comparable<Employee> {
        private int id;
        private String name;
        private int salary;
        private int workingHour;
    
        @Override public String toString()
        {
            return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", salary=" + salary + ", workingHour=" + workingHour + '}';
        }
    
        public Employee(int id, String name, int salary, int workingHour) {
            this.id = id;
            this.name = name;
            this.salary = salary;
            this.workingHour = workingHour;
        }
        @Override
        public int compareTo(Employee o) {
            if (this.workingHour> o.workingHour) {
                return 1;
            } else if (this.workingHour== o.workingHour) {
                return 0;
            } else {
                return -1;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2013-11-03
      • 2016-03-03
      相关资源
      最近更新 更多