Sorting a Collection of object without implementing the Comparator/Comparable
This java program describes how to sort collection of user defined objects using Comparator as anonymous class(without implementing the Comparator interface)
There are two ways we can sort the List of user defined objects in java
1. Implement the Comparator/Comparable interface by userdefined class
2. Without implementing Comparator interface(using anonymous class)
Now am going to show the example which will sort the userdefined objects using
Comparator interface as anonymous class(ie. without implementing the Comparator interface)
My Model Object : Sample.java
package com.vels.test;
/**
* @author velmurugan pousel
* created on Jul 21, 2011 for test
*/
* @author velmurugan pousel
* created on Jul 21, 2011 for test
*/
public class Sample {
public String id;
public String name;
public int age;
Sample(String ID, String nm, int ag){
this.id = ID;
this.name = nm;
this.age = ag;
}
public String name;
public int age;
Sample(String ID, String nm, int ag){
this.id = ID;
this.name = nm;
this.age = ag;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
Now am going to sort the list of Sample class object based on the ID field.
Here is my main method :
package com.vels.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author velmurugan pousel
* created on Jul 21, 2011 for test
*/
* @author velmurugan pousel
* created on Jul 21, 2011 for test
*/
public class ComparatorTest {
public static void main(String[] args) {
List<Sample> list = new ArrayList<Sample>();
Sample s1 = new Sample("1", "amin", 12);
list.add(s1);
s1 = new Sample("5", "ver", 21);
list.add(s1);
s1 = new Sample("1", "hji", 14);
list.add(s1);
s1 = new Sample("7", "loli", 24);
list.add(s1);
s1 = new Sample("3", "zuri", 12);
list.add(s1);
list.add(s1);
s1 = new Sample("1", "hji", 14);
list.add(s1);
s1 = new Sample("7", "loli", 24);
list.add(s1);
s1 = new Sample("3", "zuri", 12);
list.add(s1);
s1 = new Sample("1", "zuri", 12);
list.add(s1);
list.add(s1);
for (Sample s : list) {
System.out.println(" id..." + s.getId() + " name..." + s.getName());
}
System.out.println(" id..." + s.getId() + " name..." + s.getName());
}
// here is the most important part
Collections.sort(list, new Comparator<Sample>() {
//string comparation here ID is the string field
public int compare(Sample s1, Sample s2) {
int result = s1.getId().compareTo(s2.getId());
return result;
}
//string comparation here ID is the string field
public int compare(Sample s1, Sample s2) {
int result = s1.getId().compareTo(s2.getId());
return result;
}
} );
//After sorting
for (Sample s : list) {
System.out.println(" name..." + s.getName());
System.out.println(" id..." + s.getId());
}
System.out.println(" id..." + s.getId());
}
}
}
Output : Before Sorting
id...1 name...amin
id...5 name...ver
id...1 name...hji
id...7 name...loli
id...3 name...zuri
id...1 name...zuriAfter Sorting :
id...1 name...amin
id...1 name...hji
id...1 name...zuri
id...3 name...zuri
id...5 name...ver
id...7 name...loli..
To compare the int type
Collections.sort(list, new Comparator<Sample>() {
//string comparation here ID is the string field
public int compare(Sample s1, Sample s2) {
int result = s1.getAge() - s2.getAge();
return result;
}
//string comparation here ID is the string field
public int compare(Sample s1, Sample s2) {
int result = s1.getAge() - s2.getAge();
return result;
}
} );
No comments:
Post a Comment