首页 > 编程语言 >利用Apache的commons-beanutils和commons-collections包实现Java对象的按属性排序

利用Apache的commons-beanutils和commons-collections包实现Java对象的按属性排序

时间:2022-12-02 11:37:48浏览次数:39  
标签:username Java users comparator commons collections password public String


在日常工作中经常用到需要对java对象集合或者Map集合中的某个属性做排序,这个需求可以利用Apache的commons-beanutils和commons-collections包来实现,主要实现方式如下:

public static <T> void sort(List<T> 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));
}


下面看看一个具体实现例子,其中有按对象属性排序和Map某个key排序

首先是一个Java对象

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;
}
}

排序测试类

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<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 5; i++) {
Map<String, Object> user = new LinkedHashMap<String, Object>();
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<User> users = new ArrayList<User>();
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 <T> void sort(List<T> 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));
}
}

标签:username,Java,users,comparator,commons,collections,password,public,String
From: https://blog.51cto.com/u_14230175/5906746

相关文章

  • word 97+java+freemarker导出word
    java中,导出word,一般是可以先用word做好模版,然后另存为mht网页文件,然后利用freemaker中做标记,再用java程序填充,但要注意,保存为网页文件后,要改下编......
  • 异常解决 Caused by: java.lang.NoSuchMethodError: kotlin.collections.ArraysKt.c
    问题描述:引入minio8.3.5的包后,项目启动失败,异常信息如下***************************APPLICATIONFAILEDTOSTART***************************Description:Anat......
  • Java删除ArrayList中的重复元素的2种方法
    ArrayList是Java中最常用的集合类型之一。它允许灵活添加多个null元素,重复的元素,并保持元素的插入顺序。在编码时我们经常会遇到那种必须从已建成的Ar......
  • apache commons包中的commons-beanutils
    publicclassEntity{privateIntegerid=5;privateStringname="rongxinhua";publicIntegergetId(){returnid;}publicvoidsetId(......
  • Java生成UUID
    Java生成UUID一、UUID简介UUID含义是通用唯一识别码(UniversallyUniqueIdentifier),这是一个软件建构的标准。也是被开源软件基金会(OpenSoftwareFoundation,OSF......
  • 小tips:设置java 的heap
    在java中,可以设置heap的大小:-Xms<size>设置初始heap的大小-Xmx<size> 设置最大的heap的大小-xss<size>设置threadstack大小如下文,设......
  • java 调用系统指令
    Stringmsg="";Processprocess=null;try{ProcessBuilderpb=newProcessBuilder();//因为process执行命令并不是......
  • java 调用 ffmpeg 指令获取视频的第 n 帧图片-兼容【linux 和 win】
    1.调用if(StringUtil.equals(HotCacheData.os,"linux")){msg=processVideo(filePath,n,targetUrl);}else{if(StringUti......
  • javascript中generator快速小结
    1基本例子  function*generatorFunc(){console.log("任务一");yield1;console.log("任务二");yield*generatorSubFunc();console.log("任务三");return......
  • java中求两个日期之间的天数
    收藏一个常用的代码段,那就是java中求两个日期间的天数,常见于日历中的两个日期(yyyy-mm-dd)格式,要求它们之间的相差的天数,例子如下:CalendarstartC......