user1502381
user1502381

Reputation: 145

Nesting arbitrary objects in 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.

Upvotes: 3

Views: 686

Answers (5)

Matt
Matt

Reputation: 11805

Use Arrays.sort() with different Comparator implementations as noted above. Keep in mind that this method (as well as Collections.sort()) operate on the data in place. So if you need to keep getting at different views of the data, you may want to duplicate the array and sort it different ways, and hold onto each view.

This would be more efficient (CPU-wise) than constantly re-sorting the array.

Upvotes: 0

Anshul
Anshul

Reputation: 806

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.

Upvotes: 2

Sujay
Sujay

Reputation: 6783

You might want to read this link to understand Object Ordering in Java

The next thing that you want to do is use an ArrayList and add all your employees in this. You would then want to do something like this:

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

I hope this helps!

Upvotes: 0

Dave Richardson
Dave Richardson

Reputation: 4995

Instead of an array use an ArrayList to hold your Employees. You can then use Collections.sort() to sort that list. There are two versions of sort(), one of which takes a Comparator that will allow you to sort in your desired order.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35011

Check out java.util.Comparator<T> (the T in this case would be your Employee type)

You can create different Comparators and use Collections.sortList or Arrays.sort(...) (these methods are pseudo methods - look up the exact versions from the docs)

Upvotes: 0

Related Questions