Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting arbitrary objects in Java

Tags:

java

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.

like image 494
user1502381 Avatar asked Jun 06 '26 04:06

user1502381


1 Answers

You should use ArrayList in place of Arrays.

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

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

Now write down custom Comparator

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;
    }
}

Similarly you can write for other comparisions.

To sort them now, use:

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

This will solve your problem.

like image 50
Anshul Avatar answered Jun 08 '26 18:06

Anshul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!