3

I am having trouble solving a particular problem in Java (which I did not find by search). I do not know how to create a nested lists of objects - with a different type of object/primitive type at the end. For example:

*Note: only an example. I am actually doing this below with something other than Employee, but it serves as simple example.

I have an array of an object Employee. It contains information on the Employee.

public class Employee {
    int age
    int salary
    int yearsWorking
    public Employee () {
        // constructor...
    }
    // Accessors
}

What I need to do is organize the Employees by quantiles/percentiles. I have done so by the following:

import org.apache.commons.math.stat.descriptive.rank.Percentile;
public class EmployeeSort {
    public void main(String args[]) {
        Percentile p = new Percentile();
        Employee[] employeeArray = new Employee[100];
        // filled employeeArray
        double[] ageArray = new double[100];
        // filled ageArray with ages from employeeArray
        int q = 25; // Percentile cutoff
        for (int i = 1; i*q < 100; i++) {
            // assign percentile cutoff to some array to contain the values
        }
    }
}

Now, the problem I have is that I need to organize the Employees first by the percentiles of age, then percentiles of yearsWorking, and finally by percentiles of salary. My Java knowledge is inadequate right now to solve this problem, but the project I was handed was in Java. I am primarily a python guy, so this problem would have been a lot easier in that language. No such luck.

Edit: So I notice some confusion on whether I need ordering, and some people suggesting use of Comparator. I use Comparator because percentiles requires an ordered list. The problem I am having is how to store the binning of the Employee into their appropriate percentile. To clarify, I need the Employee object binned together into their appropriate percentile by age. Then, within that bin of age, I need to bin all those Employee objects within percentiles for Employee.yearsWorking. Following that, within a given percentile bin of yearsWorking which is within a given percentile bin of Employee.age, I need to create percentile bins of Employee.salary.

4

5 回答 5

2

您应该使用ArrayList 代替 Arrays。

ArrayList<Employee> employeeList= new ArrayList<Employee>();

for (int i = 0, i <= employeeArray.length; i++)
    employeeList.add(employeeArray[i]);

现在写下自定义比较器

public class EmployeeComparatorByAge<Employee>
{
    public int compare(Object o1, Object o2)
    {
        if (o1 != null && o2!= null && o1 instanceof Employee && o2 instanceof Employee )
            return ((Employee)o1).age - ((Employee)o2).age;
        else
            return -1;
    }
}

同样,您可以编写其他比较。

现在要对它们进行排序,请使用:

Collections.sort(employeeList, new EmployeeComparatorByAge<Employee>());

这将解决您的问题。

于 2012-07-05T15:31:06.517 回答
0

您可能想阅读此链接以了解Java 中的对象排序

您要做的下一件事是使用 ArrayList 并将您的所有员工添加到其中。然后你会想做这样的事情:

ArrayList<Employee> employeeList= new ArrayList<Employee>();
/**
  * Add your employee objects to the list
  */


Collections.sort(contacts, new Comparator<Employee>() {
    public int compare(Employee emp1, Employee emp2) {
        int returnValue;
        /**
         * Your comparison logic
         */

        return returnValue;
    }
}); 

我希望这有帮助!

于 2012-07-05T15:13:43.837 回答
0

如上所述,将 Arrays.sort() 与不同的 Comparator 实现一起使用。请记住,此方法(以及 Collections.sort())对数据进行操作。因此,如果您需要不断获取数据的不同视图,您可能需要复制数组并以不同方式对其进行排序,并保留每个视图。

这将比不断重新排序数组更有效(CPU 方面)。

于 2012-07-05T17:20:18.930 回答
0

查看 java.util.Comparator<T> (在这种情况下, T 将是您的 Employee 类型)

您可以创建不同的比较器并使用 Collections.sortList 或 Arrays.sort(...) (这些方法是伪方法 - 从文档中查找确切版本)

于 2012-07-05T15:11:44.033 回答
0

代替数组使用 anArrayList来保存您的员工。然后,您可以使用Collections.sort()对该列表进行排序。sort() 有两个版本,其中一个采用Comparator允许您按所需顺序排序的版本。

于 2012-07-05T15:12:35.777 回答