Archive for category Collections

Ordering a list of Objects

This Java tip relates to functionality that has been around for quite a while but a lot of people are still not aware of it.

There are many occasions when you may need to order a list of data objects. Luckily the Java Collections Framework provides utilites to allow objects to be easily ordered.

The Comparable interface declares one interface:
public int compareTo(Object o);

The data object that you wish to compare must implement this interface. This method should return either -1, 0, or 1.
When the two objects are compared, the compareTo() method is called, passing the object that you wish to compare it to.

If the object is greater a 1 is returned, -1 will indicated that it is less and 0 will indicate that they are equal. In the ComparablePerson class below we have implemented the compareTo method. We will persume that all getter and setter methods are created.

public class ComparablePerson implements Comparable
{
private int age;
private String firstName = "";
private String secondName = "";

public ComparablePerson(String firstName, String secondName, int age)
{
this.firstName = firstName;
this.secondName = secondName;
this.age = age;
}

public int compareTo(Object o)
{
int result = -1;
if (o instanceof ComparablePerson)
{
ComparablePerson compareObj = (ComparablePerson) o;
// Compare by Age
result = new Integer(this.getAge()).compareTo(new Integer(
compareObj.getAge()));
if (result == 0)
{
result = this.getSecondName().compareTo(
compareObj.getSecondName());
if (result == 0)
{
result = this.getFirstName().compareTo(
compareObj.getFirstName());
}
}
}
return result;
}
}
....

In the compareTo method the we first check to see that the object that we wish to compare a ComparablePerson object. If it is not then we cannot compare the properties.

We first compare based on the age. There is no point writing a method to compare two int values as this is already implemented in the Integer compareTo method.

At this stage if the the objects are still equal we continue to compare based on the secondName property. We use the compareTo method of the String class to perform this check.

Finally if they are still equal we should check the firstName property.

This object can now be compared to another ComparablePerson object.
The Collections API provides methods that will sort or reverse a list of compareable objects. The CompareTest class will create a list of ComparablePerson objects and then sort the list.

public class CompareTest
{
public static void main(String[] args)
{
ComparablePerson john = new ComparablePerson("John", "Doe", 31);
ComparablePerson paul = new ComparablePerson("Paul", "Doe", 31);
ComparablePerson stephen = new ComparablePerson("Stephen", "Downey", 27);
ArrayList peopleList = new ArrayList();
peopleList.add(john);
peopleList.add(paul);
peopleList.add(stephen);
System.out.println(peopleList);
Collections.sort(peopleList);
System.out.println(peopleList);
Collections.reverse(peopleList);
System.out.println(peopleList);
}
}
...

The output is as follows:

[John Doe 31, Paul Doe 31, Stephen Downey 27]
[Stephen Downey 27, John Doe 31, Paul Doe 31]
[Paul Doe 31, John Doe 31, Stephen Downey 27]

No Comments