How to sort List of Bean in Java?
UPDATED: 06 December 2014
Tags:
Collection
,
Comparator
,
List
In my previous article How to sort a List in Java? We have seen how we can sort List<String>. This time we'll understand code of How we can sort List<Bean>.
Storing data in bean is common practice in current industry. Bean structure allows us to get specific property of object. Hope you all are aware of what is bean so lets not waste time and understand the code.
Background
We have User object that holds two property Firstname and Lastname. We will sort the List on Firstname in ascending order. There are two technique you can use to sort an object and we'll checkout one by one.
Technique One
Say you have access to your bean class and can modify the class file then by implementing Comparable interface you can perform sorting.
/** * @author javaQuery */ public class User implements Comparable<User> { private String Firstname; private String Lastname; public String getFirstname() { return Firstname; } public void setFirstname(String Firstname) { this.Firstname = Firstname; } public String getLastname() { return Lastname; } public void setLastname(String Lastname) { this.Lastname = Lastname; } /** * This `compareTo` used to compare two `User` object, * Which internally uses `compareTo` of String class to compare two String values. * @param obj * @return */ @Override public int compareTo(User obj) { /* Check object and Firstname are not null */ if(obj != null && obj.Firstname != null){ /* We will use `compareTo` of String class */ /* For ascending order */ return this.Firstname.compareTo(obj.Firstname); /* For descending order */ /* return obj.Firstname.compareTo(this.Firstname) */ }else{ return -1; } } /* If you want to sort on `int` value then uncomment following code */ // @Override // public int compareTo(User obj) { // /* Check object and id is not null and 0 */ // if (obj != null && obj.getId() != 0) { // /* For ascending order */ // return this.getId() - obj.getId(); // // /* For descending order */ // /* return obj.getId() - this.getId(); */ // } else { // return -1; // } // } }Source Code
import com.javaquery.beans.User; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author javaQuery */ public class SortingObjectListExample { public static void main(String[] args) { /* Create object of User */ User user1 = new User(); /* Set Firstname of User */ user1.setFirstname("Vicky"); /* Set Lastname of User */ user1.setLastname("Thakor"); User user2 = new User(); user2.setFirstname("Chirag"); user2.setLastname("Thakor"); User user3 = new User(); user3.setFirstname("Heer"); user3.setLastname("Thakor"); User user4 = new User(); user4.setFirstname("Yogita"); user4.setLastname("Thakor"); User user5 = new User(); user5.setFirstname("Riddhi"); user5.setLastname("Thakor"); User user6 = new User(); user6.setFirstname("Xender"); user6.setLastname("Thakor"); /* Add Users in List<User> */ List<User> listUser = new ArrayList<User>(); listUser.add(user4); listUser.add(user6); listUser.add(user1); listUser.add(user5); listUser.add(user2); listUser.add(user3); System.out.println("Initial List"); System.out.println("------------------------"); for (User user : listUser) { System.out.println(user.getFirstname() + " " + user.getLastname()); } System.out.println("------------------------"); Collections.sort(listUser); System.out.println("List after Collections.sort(List<T> list)"); System.out.println("------------------------"); for (User user : listUser) { System.out.println(user.getFirstname() + " " + user.getLastname()); } } }Output of the above code is given at the end of the article as both technique gives same output.
Technique Two
Say if you don't have access to your bean(bind in jar) and still you want to sort on specific property then by creating custom Comparator you can achieve same.
/** * @author javaQuery */ public class User{ private String Firstname; private String Lastname; public String getFirstname() { return Firstname; } public void setFirstname(String Firstname) { this.Firstname = Firstname; } public String getLastname() { return Lastname; } public void setLastname(String Lastname) { this.Lastname = Lastname; } }Source code
import com.javaquery.beans.User; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * @author javaQuery */ public class SortingObjectListExample { public static void main(String[] args) { /* Create object of User */ User user1 = new User(); /* Set Firstname of User */ user1.setFirstname("Vicky"); /* Set Lastname of User */ user1.setLastname("Thakor"); User user2 = new User(); user2.setFirstname("Chirag"); user2.setLastname("Thakor"); User user3 = new User(); user3.setFirstname("Heer"); user3.setLastname("Thakor"); User user4 = new User(); user4.setFirstname("Yogita"); user4.setLastname("Thakor"); User user5 = new User(); user5.setFirstname("Riddhi"); user5.setLastname("Thakor"); User user6 = new User(); user6.setFirstname("Xender"); user6.setLastname("Thakor"); /* Add Users in List<User> */ List<User> listUser = new ArrayList<User>(); listUser.add(user4); listUser.add(user6); listUser.add(user1); listUser.add(user5); listUser.add(user2); listUser.add(user3); System.out.println("Initial List"); System.out.println("------------------------"); for (User user : listUser) { System.out.println(user.getFirstname() + " " + user.getLastname()); } System.out.println("------------------------"); /* Create object of `SortingObjectListExample` */ SortingObjectListExample objSortingObjectListExample = new SortingObjectListExample(); /* Get custom Comparator */ Comparator<User> objComparator = objSortingObjectListExample.getComparator(); /* Sort List<User> using custom Comparator */ Collections.sort(listUser, objComparator); System.out.println("List after Collections.sort(List<T> list)"); System.out.println("------------------------"); for (User user : listUser) { System.out.println(user.getFirstname() + " " + user.getLastname()); } } /* Create custom Comparator */ public Comparator<User> getComparator() { return new Comparator<User>() { @Override public int compare(User obj1, User obj2) { /* Check obj1 and obj2 are not null and also check Firstname of both objects are not null */ if (obj1 != null && obj2 != null && obj1.getFirstname() != null && obj2.getFirstname() != null) { /* Use `compareTo` of String class to compare two String values */ /* For ascending order */ return obj1.getFirstname().compareTo(obj2.getFirstname()); /* For descending order */ /* return obj2.getFirstname().compareTo(obj1.getFirstname()) */ } else { return -1; } } }; } }Output
Initial List ------------------------ Yogita Thakor Xender Thakor Vicky Thakor Riddhi Thakor Chirag Thakor Heer Thakor ------------------------ List after Collections.sort(List<T> list) ------------------------ Chirag Thakor Heer Thakor Riddhi Thakor Vicky Thakor Xender Thakor Yogita Thakor
Tags:
Collection
,
Comparator
,
List
0 comments :