前言
对list<泛型>进行指定字段排序及分页,利用反射实现排序。
一、代码实现
代码如下:
package com.ld.util;
import com.ld.entity.PageEntity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Description: 对list集合进行分页排序
* @Author: ld
* @Date: 2021-02-02 15:29
*/
public class PageUtil {
private PageUtil() {
}
/**
* 对List进行排序操作
*
* @param list 需要排序的列表
* @param orderBy 需要进行排序的对象字段(需要含有相应的get方法)
* @param sort asc 正序,desc 反序
* @param <E> 泛型
*/
public static <E> void sort(List<E> list, String orderBy, String sort) {
//1.获取指定字段的get方法名
String methodName = "get" + orderBy.toUpperCase().substring(0, 1).toUpperCase() + orderBy.substring(1);
list.sort((a, b) -> {
int ret = 0;
try {
//2.获取方法
Method m1 = a.getClass().getMethod(methodName, null);
Method m2 = b.getClass().getMethod(methodName, null);
String rs1 = null;
String rs2 = null;
//3.获取方法返回值
if (m1.invoke(a, null) != null) {
rs1 = m1.invoke(a, null).toString();
}
if (m2.invoke(a, null) != null) {
rs2 = m2.invoke(b, null).toString();
}
//4.判断字符串是否为数字,若为数字则直接加减
if (rs1 != null && rs2 != null && rs1.matches("-?[0-9]+.?[0-9]*")) {
if ("desc".equals(sort)) {
ret = Integer.valueOf(rs2).compareTo(Integer.valueOf(rs1));
} else {
ret = Integer.valueOf(rs1).compareTo(Integer.valueOf(rs2));
}
} else if (rs1 != null && rs2 != null) {
if ("desc".equals(sort))//倒序
ret = rs2.compareTo(rs1);
else//正序
ret = rs1.compareTo(rs2);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ne) {
ne.printStackTrace();
}
return ret;
});
}
/**
* 分页排序
*
* @param list 待处理列表
* @param pageNum 页数
* @param pageSize 分页大小
* @param orderBy 根据什么排序
* @param sortBy 正序反序(asc、desc)
* @param <E> 任意对象
* @return 处理好的数据
*/
public static <E> List<E> divisionList(List<E> list, int pageNum, int pageSize, String orderBy, String sortBy) {
int total = list.size();
int startIndex = pageNum * pageSize - pageSize;
int endIndex = pageNum * pageSize;
if (total < startIndex) {
startIndex = total;
}
if (total < endIndex) {
endIndex = total;
}
if (orderBy != null && !"".equals(orderBy)) {
sort(list, orderBy, sortBy);
}
return list.subList(startIndex, endIndex);
}
}
二、实现验证
1.创建实体类User
package com.ld.example;
/**
* @Description: 测试对象
* @Author: ld
* @Date: 2021-02-02 17:10
*/
public class User {
private Integer id;
private String name;
private String age;
private String school;
private String home;
private String telephone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", school='" + school + '\'' +
", home='" + home + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
2.创建单元测试
package com.ld;
import com.ld.entity.PageEntity;
import com.ld.example.User;
import com.ld.util.PageUtil;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @Description:
* @Author: ld
* @Date: 2021-02-02 17:14
*/
public class test {
String[] strings = new String[]{"a","av","cc","av","ad","xd","va","bc","de","dx"};
List<User> init(){
List<User> list = new ArrayList<>();
for (int i=0;i<strings.length;i++){
int max=100,min=1;
int random = (int) (Math.random()*(max-min)+min);
User user = new User();
user.setId(i);
user.setName(strings[i]);
user.setAge("age_"+strings[i]);
user.setHome("home_"+strings[i]);
user.setSchool("school_"+strings[i]);
user.setTelephone(random+"");
list.add(user);
}
System.out.println("-------------------------初始化数据如下----------------------------");
list.forEach(System.out::println);
return list;
}
/**
* 测试排序
*/
@Test
public void testSort(){
List<User> inList = init();
String orderBy="telephone";
String sortBy = "desc";
//根据条件从list中查询回具体数据
PageUtil.sort(inList,orderBy,sortBy);
System.out.println("------------------------排序后结果如下-------------------------");
inList.forEach(System.out::println);
}
/**
* 测试分页排序
*/
@Test
public void testDivisionList(){
List<User> inList = init();
int pageNum = 2;
int pageSize = 3;
String orderBy="telephone";
String sortBy = "asc";
//根据条件从list中查询回具体数据
List<User> users = PageUtil.divisionList(inList,pageNum,pageSize,orderBy,sortBy);
users.forEach(System.out::println);
}
}
3.效果展示
排序
当前以telephone字段倒序排序,有兴趣的话可尝试其他字段排序。
分页
当前以telephone字段正序排序后分页,每页大小为3条数据,显示第二页数据
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。