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 ComparatorLast but not least, lets write a test class.{ /** 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()); } }
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{ ArrayListdeliveries = 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
Most helpful packers and mvoers website for home shifting:
ReplyDeleteNoida packers and movers
Delhi packers and movers
Gurgaon packers and movers
Faridabad packers and movers
Ghaziabad packers and movers
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
ReplyDelete09024332029,9310051422 Packers and movers in noida
Superb Blog.
ReplyDeletealso, join Java course in Pune