java如何对list进行排序_Java开发经验分享之如何给List集合进行排序。自定义排序的类,专门针对列表(List)中的日期数据进行排序;也可按指定属性进行。...
发布日期:2021-06-24 11:41:20 浏览次数:2 分类:技术文章

本文共 4052 字,大约阅读时间需要 13 分钟。

很实用的一个List集合排序类。(本作品为原创,如有不足之处,还望大牛多给意见。如需转载,请注明出处。谢谢!)

一、根据List 中的Student对象中的开始时间进行排序。注意,该类只给Date类型属性字段进行排序。代码如下:

说明:

自定义类名:DateSortList ,方法名:sortByAttribute,参数1:List,即你需要进行排序的List。参数2:String attribute,即你需要进行排序的字段,Student对象中的时间属性,例如:开始时间-->startDate属性。当然也可以为数据的创建时间。只要是Date类型参数即可。参数3:boolean reverseFlag ,排序规则。true为倒序。。

public classDateSortList {private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");/*** 对列表中的数据按指定日期字段进行排序。

*

*@paramlist

*@paramattribute 排序的时间字段,例如Vo类中的开始时间字段:startDate;

*@paramreverseFlag true:倒序*/@SuppressWarnings({"unchecked", "rawtypes"})public static void sortByAttribute(List list, final String attribute,final booleanreverseFlag) {

Collections.sort(list,new Comparator() {public intcompare(Object arg1, Object arg2) {int result = 0;try{

Field f1=arg1.getClass().getDeclaredField(attribute);

f1.setAccessible(true);

Field f2=arg2.getClass().getDeclaredField(attribute);

f2.setAccessible(true);

String s1=DateUtil.convertDateToString(

(Date) f1.get(arg1),"yyyy-MM-dd");

String s2=DateUtil.convertDateToString(

(Date) f2.get(arg2),"yyyy-MM-dd");if (s1 == null || s1.equals("")) {

s1= "1900-00-00";

}if (s2 == null || s2.equals("")) {

s2= "1900-00-00";

}long l = sdf.parse(s1).getTime() -sdf.parse(s2).getTime();if (l > 0) {

result= 1;

}else if (l < 0) {

result= -1;

}else{

result= 0;

}if(reverseFlag) {//倒序

result = -result;

}

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}

});

}

}

二、根据List 中的Student对象中的Int、Date、Long三种类型属性字段进行排序,代码如下:

说明:该方法是在上面的方法中进行拓展,既然可以根据Date类型进行排序,那么其他类型呢?这里拓展了Int、long,两种类型。

自定义类名:DateSortList ,方法名:sortByAttribute,参数1:List,即你需要进行排序的List。参数2:String attribute,即你需要进行排序的字段,Student对象中的时间属性,例如:开始时间-->startDate属性。当然也可以为数据的创建时间。只要是Date类型参数即可。参数3:boolean reverseFlag ,排序规则。true为倒序。参数4: String type

即为需要进行排序字段的类型。这里拓展了Int、long,两种类型。

/*** 对列表中的数据按指定字段类型进行排序。

*

*@paramlist

* 需要排序的列表

*@paramattribute

* 字段id

*@paramreverseFlag

* 是否倒序

*@paramtype:

* Int、Date、Long*/@SuppressWarnings({"unchecked", "rawtypes"})public static void sortByAttribute(List list, finalString attribute,final boolean reverseFlag, finalString type) {

Collections.sort(list,new Comparator() {public intcompare(Object arg1, Object arg2) {int result = 0;try{

Field f1=arg1.getClass().getDeclaredField(attribute);

f1.setAccessible(true);

Field f2=arg2.getClass().getDeclaredField(attribute);

f2.setAccessible(true);if ("date".equals(type)) {

String s1=(String) f1.get(arg1);

String s2=(String) f2.get(arg2);if (s1 == null || s1.equals("")) {

s1= "1900-00-00";

}if (s2 == null || s2.equals("")) {

s2= "1900-00-00";

}long l =sdf.parse(s1).getTime()-sdf.parse(s2).getTime();if (l > 0) {

result= 1;

}else if (l < 0) {

result= -1;

}else{

result= 0;

}

}else if ("int".equals(type)) {

Integer s1=(Integer) f1.get(arg1);

Integer s2=(Integer) f2.get(arg2);if (s1 == null) {

s1= -9999;

}if (s2 == null) {

s2= -9999;

}int l = s1 -s2;if (l > 0) {

result= 1;

}else if (l < 0) {

result= -1;

}else{

result= 0;

}

}else if ("long".equals(type)) {

Long s1=(Long) f1.get(arg1);

Long s2=(Long) f2.get(arg2);if (s1 == null) {

s1= -9999L;

}if (s2 == null) {

s2= -9999L;

}long l = s1 -s2;if (l > 0) {

result= 1;

}else if (l < 0) {

result= -1;

}else{

result= 0;

}

}else if ("string".equals(type)) {

String s1=(String) f1.get(arg1);

String s2=(String) f2.get(arg2);if (s1 == null || s1.equals("")) {

s1= "0";

}if (s2 == null || s2.equals("")) {

s2= "0";

}int l = Integer.parseInt(s1) -Integer.parseInt(s2);if (l > 0) {

result= 1;

}else if (l < 0) {

result= -1;

}else{

result= 0;

}

}if(reverseFlag) {//倒序

result = -result;

}

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}

});

}

三、给List去重且不改变顺序,代码如下:

说明:开发过程中,有很多小伙伴想给一个List集合去掉重复。我们都知道集合中的Set即可去重。

为了方便代码管理性,通用性。工具类肯定是少不了的。代码如下:

public static List removeDuplicateWithOrder(Listlist) {

List resultList = new ArrayList();

Set set = new HashSet();

Iterator iterator =list.iterator();while(iterator.hasNext()) {

String element=iterator.next().toString();if(set.add(element)) {

resultList.add(element);

}

}returnresultList;

}

原文:https://www.cnblogs.com/JimYi/p/10330039.html

转载地址:https://blog.csdn.net/weixin_32553639/article/details/114142738 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java web测试环境搭建_Selenium+ Webdriver+JAVA 自动化测试 环境搭建( SELENIUM自动化测试入门基础)...
下一篇:netbeans java db_在NetBeans IDE中使用Java DB数据库

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月16日 08时08分12秒