list.stream().sorted常用之关于对象属性排序aa.compareTo(bb)

先补充下对象比较,再说排序:
 

Integer类型比较:

Integer aa=1;
Integer bb=3;
System.out.println("integer的比较");
System.out.println(aa+ ".compareTo("+ bb +"):  "+  aa.compareTo(bb));
aa=3;
System.out.println(aa+ ".compareTo("+ bb +"):  "+  aa.compareTo(bb));
aa=5;
System.out.println(aa+ ".compareTo("+ bb +"):  "+  aa.compareTo(bb));

输出:

integer的比较
1.compareTo(3):  -1
3.compareTo(3):  0
5.compareTo(3):  1

结论:小的数字小于大的数字返回-1,相等返回0,大于返回1

 

 

LocalDateTime类型比较:

System.out.println("LocalDateTime的比较");
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 3, 24, 7, 0, 0);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 3, 24, 12, 0, 0);
System.out.println(localDateTime1+ ".compareTo("+ localDateTime2 +")  :"+  localDateTime1.compareTo(localDateTime2));

localDateTime1 = LocalDateTime.of(2020, 3, 24, 12, 0, 0);
System.out.println(localDateTime1+ ".compareTo("+ localDateTime2 +")  :"+  localDateTime1.compareTo(localDateTime2));

localDateTime1 = LocalDateTime.of(2020, 3, 24, 20, 0, 0);
System.out.println(localDateTime1+ ".compareTo("+ localDateTime2 +")  :"+  localDateTime1.compareTo(localDateTime2));

输出:

LocalDateTime的比较
2020-03-24T07:00.compareTo(2020-03-24T12:00)  :-1
2020-03-24T12:00.compareTo(2020-03-24T12:00)  :0
2020-03-24T20:00.compareTo(2020-03-24T12:00)  :1

 

结论:小的日期小于大的日期返回-1,相等返回0,大于返回1

 

 

 

list.stream().sorted常用排序两种方式:

 

方式1:匿名内部类方式,重写compare方法,也可以用lambda表达式

 


    @Test
    public void testSortByLocalDateTime2(){
        ArrayList<SBox> sBoxes = new ArrayList<>();
        for (int i = 1; i <8 ; i++) {
            SBox sBox = new SBox();
            Random random = new Random();
            int nextInt = random.nextInt(15) + i;
            //System.out.println(nextInt);
            sBox.setStreamBoxId("sboxid" + i);
            sBox.setStartTime(LocalDateTime.of(2020,3,24,nextInt,0,0));
            sBoxes.add(sBox);
        }
        //stream 排序默认升序
        List<SBox> collect = sBoxes.stream().sorted((s1, s2) -> s1.getStartTime().compareTo(s2.getStartTime())).collect(Collectors.toList());
        System.out.println("排序前:");
        for (SBox sBox : sBoxes) {
            System.out.println(sBox);
        }
        System.out.println("------------");
        System.out.println("排序后升序");
        for (SBox box : collect) {
            System.out.println(box);
        }
        //stream 排序降序操作
        //思路:先升序,再用Collections.reverse(list)方法颠倒排序
        List<SBox> collect1 = sBoxes.stream().sorted((s1, s2) -> s1.getStartTime().compareTo(s2.getStartTime())).collect(Collectors.toList());
        Collections.reverse(collect1);
        System.out.println("排序后降序");
        for (SBox box : collect1) {
            System.out.println(box);
        }
    }

 

输出:

排序前:
SBox{streamBoxId='sboxid1', streamLineId='null', startTime=2020-03-24T05:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid2', streamLineId='null', startTime=2020-03-24T06:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid3', streamLineId='null', startTime=2020-03-24T03:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid4', streamLineId='null', startTime=2020-03-24T15:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid5', streamLineId='null', startTime=2020-03-24T19:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid6', streamLineId='null', startTime=2020-03-24T08:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid7', streamLineId='null', startTime=2020-03-24T14:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
------------
排序后升序
SBox{streamBoxId='sboxid3', streamLineId='null', startTime=2020-03-24T03:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid1', streamLineId='null', startTime=2020-03-24T05:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid2', streamLineId='null', startTime=2020-03-24T06:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid6', streamLineId='null', startTime=2020-03-24T08:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid7', streamLineId='null', startTime=2020-03-24T14:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid4', streamLineId='null', startTime=2020-03-24T15:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid5', streamLineId='null', startTime=2020-03-24T19:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
排序后降序
SBox{streamBoxId='sboxid5', streamLineId='null', startTime=2020-03-24T19:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid4', streamLineId='null', startTime=2020-03-24T15:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid7', streamLineId='null', startTime=2020-03-24T14:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid6', streamLineId='null', startTime=2020-03-24T08:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid2', streamLineId='null', startTime=2020-03-24T06:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid1', streamLineId='null', startTime=2020-03-24T05:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}
SBox{streamBoxId='sboxid3', streamLineId='null', startTime=2020-03-24T03:00, endTime=null, capability=null, useState=null, opState=null, setId='null', reqId='null', specId='null', preSetId='null', preUseCapability=null, lastSetId='null', lastUseCapability=null}

 

 

 

方式2: Comparator.comparing  推荐使用的方式

//升序
sBoxes.stream().sorted(Comparator.comparing(SBox::getStartTime)).collect(Collectors.toList());
//降序
sBoxes.stream().sorted(Comparator.comparing(SBox::getStartTime).reversed()).collect(Collectors.toList());

 

发布了157 篇原创文章 · 获赞 3 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览