Data AccessCore JavaApp FrameworksViewTestingBuildingDeploymentDev ToolsSecurityOpinions

Wednesday, March 4, 2009

Sorting java.util.List with a java.util.Comparator using java.util.Collections

Sorting objects in a List is a piece of code that every java programmer will write more than once in their lifetime. Any object that implements the java.util.List interface can utilize the sort method contained within the java.util.Collections class to sort the list, using a java.util.Comparator.

Below is a short tutorial and code sample.

First, let's define our object model.

package com.company.model;

public class MilkDelivery {

private int daysTilExpiration;

public MilkDelivery(int daysTilExpiration){ 
this.daysTilExpiration = daysTilExpiration; 
}

public int getDaysTilExpiration() { 
return daysTilExpiration; 
}

public void setDaysTilExpiration(int daysTilExpiration) { 
this.daysTilExpiration = daysTilExpiration; }
}


Second, let's write a Comparator and implement the compare method.

package com.company.model.comparator;

import java.util.Comparator;

import com.company.model.MilkDelivery;

public class MilkDeliveryComparator implements Comparator {

/** Supports sorting from days til expiration ascending */
public int compare(MilkDelivery o1, MilkDelivery o2) {

//Cast down
MilkDelivery delivery1 = (MilkDelivery) o1;
MilkDelivery delivery2 = (MilkDelivery) o2;

if(delivery1.getDaysTilExpiration() > delivery2.getDaysTilExpiration()){
return 1;    
}else if(delivery1.getDaysTilExpiration() == delivery2.getDaysTilExpiration()){
return 0;
}else if(delivery1.getDaysTilExpiration() < delivery2.getDaysTilExpiration()){
return -1;

//or a much more graceful solution....
//return (delivery1.getDaysTilExpiration() - delivery2.getDaysTilExpiration());
}
}
Last but not least, lets write a test class.
package com.company.test;

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

import com.company.model.MilkDelivery;
import com.company.model.comparator.MilkDeliveryComparator;

public class MilkTest {

public static void main(String [] args) throws Exception{

ArrayList deliveries = new ArrayList();

deliveries.add(new MilkDelivery(2));
deliveries.add(new MilkDelivery(3));
deliveries.add(new MilkDelivery(1));
deliveries.add(new MilkDelivery(4));

//Sort
Collections.sort(deliveries, new MilkDeliveryComparator());

for(int i = 0; i < deliveries.size(); i++){
MilkDelivery milkDelivery = deliveries.get(i);
System.out.println("Delivery(" + i + ")  Days til Expiration:" + 
milkDelivery.getDaysTilExpiration());
}
}
}


The output should read:
Delivery(0) Days til Expiration:1
Delivery(1) Days til Expiration:2
Delivery(2) Days til Expiration:3
Delivery(3) Days til Expiration:4

4 comments:


  1. This is a great website, so many people need this information, thanks for providing it. I love your color scheme too!

    Jin
    www.imarksweb.org

    ReplyDelete
  2. Along these lines you will likewise know precisely what sum you have to pay back to the payday credit organizations. Payday Loans

    ReplyDelete
  3. All the process of packers and movers is depend on packing material, a good quality packing material make damage free, scratch free and dust free shifting, without packing material we can’t imagine of safe shifting, without packing material lots of goods can be damage. So we always suggest for best quality packing material, we never thing about a shifting without packing material. Our policy is safe shifting damage free so for this we pack properly all goods with best quality packing material. We have a large network in whole India, so it is necessary to pack all kind of goods properly in long distance. So if you are planning to shift your goods from one place to another for long or short distance just ask to them about their packing materials and workers staff as well as transport vehicle. As we said above we fallow all parameters for home or office shifting, we always tray to do our best because we know the value of our customer and our business. If you are really searching a best movers and packers company for your home shifting give us that task to us, we never give a chance to scratch or damage a single goods. Our shifting cost always very reasonable and pocket friendly. call
    09024332029,9310051422 Packers and movers in noida

    ReplyDelete