Use the commons-beanutils and commons-collections packages of Apache to sort Java objects by attributes.

Source: Internet
Author: User

Use the commons-beanutils and commons-collections packages of Apache to sort Java objects by attributes.

In daily work, it is often used to sort a property in a java object set or Map set. This requirement can be achieved using the commons-beanutils and commons-collections packages of Apache, the main implementation method is as follows:

    public static 
  
    void sort(List
   
     list, String property, boolean asc) {        Comparator  comparator = ComparableComparator.getInstance();        comparator = ComparatorUtils.nullLowComparator(comparator);        if (!asc) {            comparator = ComparatorUtils.reversedComparator(comparator);        }        Collections.sort(list, new BeanComparator(property, comparator));    }
   
  

Next, let's look at a specific implementation example, including sorting by Object Attributes and sorting by a Map key.

The first is a Java object.

package my.commonsstudy.beanutils;public class User {    private String username;    private String password;    private String email;    public User(String username, String password, String email) {        this.username = username;        this.password = password;        this.email = email;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    @Override    public String toString() {        return this.username + ,  + this.password + ,  + this.email;    }}

Sorting Test class

package my.commonsstudy.beanutils;import org.apache.commons.beanutils.BeanComparator;import org.apache.commons.collections.ComparatorUtils;import org.apache.commons.collections.comparators.ComparableComparator;import java.util.*;public class ComparatorTest {    public static void main(String[] args) throws Exception {        test1();        test2();    }    private static void test1() {        List
  
   > users = new ArrayList
   
    >();        for (int i = 0; i < 5; i++) {            Map
    
      user = new LinkedHashMap
     
      ();            user.put(username, user_ + i);            user.put(password, passwd_ + i);            user.put(email, user_ + i + @localhost);            users.add(user);        }        System.out.println(users);        sort(users, username, false);        System.out.println(users);        sort(users, username, true);        System.out.println(users);    }    private static void test2() {        List
      
        users = new ArrayList
       
        (); for (int i = 0; i < 5; i++) { users.add(new User(user_ + i, passwd_ + i, user_ + i + @localhost)); } System.out.println(users); sort(users, username, false); System.out.println(users); sort(users, username, true); System.out.println(users); } public static 
        
          void sort(List
         
           list, String property, boolean asc) { Comparator
           comparator = ComparableComparator.getInstance(); comparator = ComparatorUtils.nullLowComparator(comparator); if (!asc) { comparator = ComparatorUtils.reversedComparator(comparator); } Collections.sort(list, new BeanComparator(property, comparator)); }}
         
        
       
      
     
    
   
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.