Dynamic Sorting JavaBean s

Keywords: Java Attribute Apache Oracle

In Java, if you sort objects, you can consider implementing the Comparable interface, but once the properties that need to be sorted are specified, they cannot be modified. The BeanUtils component provides support for JavaBean dynamic sorting, which allows you to specify sorting attributes at runtime. The running effect of the example is shown in the figure. BeanComparator compares two beans by specifying attributes. It can also be used to compare cascade attributes, index attributes, mapping attributes and combination attributes. BeanComparator passes the specified bean property to Comparable Comparator by default. If the comparative attribute values may have null values, a suitable Comparator or Chain should be passed to the constructor. Tip: Use the ComparatorUtils class of Collections components to achieve sorting with null values. Readers can refer to the relevant API s. (1) Write Employee class, which defines three domains: id for employee serial number, name for employee's name, salary for employee's salary, and provide corresponding get and set methods. The code is as follows:

private int id;
    private String name;
    private double salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee number:" + id + ",Employee Name:" + name + ",Employee wages:" + salary;
    }
}

(2) Write the Test class, create three Employee objects in the main() method of this class, initialize them, and then sort them using salary field. The code is as follows:

package com.mingrisoft.beanutils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;

public class Test {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Employee employee1 = new Employee();// Create employee1 object and initialize it
        employee1.setId(1);
        employee1.setName("IBM");
        employee1.setSalary(10000);
        Employee employee2 = new Employee();// Create employee2 objects and initialize them
        employee2.setId(2);
        employee2.setName("Oracle");
        employee2.setSalary(1000);
        Employee employee3 = new Employee();// Create employee3 objects and initialize them
        employee3.setId(3);
        employee3.setName("Sun");
        employee3.setSalary(100);
        List<Employee> list = new ArrayList<Employee>();// Create list objects and save all employee objects
        list.add(employee1);
        list.add(employee2);
        list.add(employee3);
        System.out.println("Before sorting:");
        for (Employee employee : list) {
            System.out.println(employee);// Output all objects
        }
        Collections.<Employee> sort(list, new BeanComparator("salary"));// Sort
        System.out.println("After ranking by salary:");
        for (Employee employee : list) {
            System.out.println(employee);// Output all objects
        }
    }
}

Psychological insight: the principle of dynamic ordering. BeanComparator implements the Comparator interface, using reflection to sort according to the specified attribute values. It is much better to use this kind of method than to realize this function by oneself. I hope readers can grasp it carefully.

Posted by High_-_Tek on Tue, 16 Jul 2019 10:14:37 -0700