Java8 stream排序数值字符串类型降序或者升序,stream groupby分组统计属性值出现多少次和一共有多少属性值出现

Java8 stream排序数值字符串类型降序或者升序,如果不对字符串进行特殊处理,会出现 "77" 排在"111"这样的数值字符串前面的情况,实际77是比111要小,因此需要对字符串处理下。

元数据:

[
{
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "福建",
                "portraitTitle": "客源分布",
                "portraitValue": "380.00",
                "ratio": "9.91"
            },
{
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "江西",
                "portraitTitle": "客源分布",
                "portraitValue": "228.00",
                "ratio": "5.94"
            },
           
 {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "甘肃",
                "portraitTitle": "客源分布",
                "portraitValue": "567.00",
                "ratio": "14.78"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "广东",
                "portraitTitle": "客源分布",
                "portraitValue": "438.00",
                "ratio": "11.42"
            },
            
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "吉林",
                "portraitTitle": "客源分布",
                "portraitValue": "277.00",
                "ratio": "7.22"
            },
            
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "陕西",
                "portraitTitle": "客源分布",
                "portraitValue": "199.00",
                "ratio": "5.19"
            },
            {
                "poiCode": "20101001",
                "poiName": "京香苑烤鸭",
                "portraitKey": "山西",
                "portraitTitle": "客源分布",
                "portraitValue": "192.00",
                "ratio": "5.01"
            },
            {
                "poiCode": "20201003",
                "poiName": "三福百货",
                "portraitKey": "贵州",
                "portraitTitle": "客源分布",
                "portraitValue": "175.00",
                "ratio": "4.56"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "江苏",
                "portraitTitle": "客源分布",
                "portraitValue": "148.00",
                "ratio": "3.86"
            },
            {
                "poiCode": "20201003",
                "poiName": "三福百货",
                "portraitKey": "湖北",
                "portraitTitle": "客源分布",
                "portraitValue": "147.00",
                "ratio": "3.83"
            }
        ]

因此使用如下代码段进行排序

source.stream().sorted(Comparator.comparing(TouristPortraitODTO::getPortraitValue,Comparator.comparingDouble(Double::parseDouble)).reversed()).limit(10).collect(Collectors.toList());

comparing第一个参数是排序字段,第二个参数是将前面的字段转成数值类型,本示例是因为数值为double类型,如果你是int类型可以使用Integer::parseInt即可,然后看看转换后的效果如下:

[
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "甘肃",
                "portraitTitle": "客源分布",
                "portraitValue": "567.00",
                "ratio": "14.78"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "广东",
                "portraitTitle": "客源分布",
                "portraitValue": "438.00",
                "ratio": "11.42"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "福建",
                "portraitTitle": "客源分布",
                "portraitValue": "380.00",
                "ratio": "9.91"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "吉林",
                "portraitTitle": "客源分布",
                "portraitValue": "277.00",
                "ratio": "7.22"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "江西",
                "portraitTitle": "客源分布",
                "portraitValue": "228.00",
                "ratio": "5.94"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "陕西",
                "portraitTitle": "客源分布",
                "portraitValue": "199.00",
                "ratio": "5.19"
            },
            {
                "poiCode": "20101001",
                "poiName": "京香苑烤鸭",
                "portraitKey": "山西",
                "portraitTitle": "客源分布",
                "portraitValue": "192.00",
                "ratio": "5.01"
            },
            {
                "poiCode": "20201003",
                "poiName": "三福百货",
                "portraitKey": "贵州",
                "portraitTitle": "客源分布",
                "portraitValue": "175.00",
                "ratio": "4.56"
            },
            {
                "poiCode": "101001",
                "poiName": "歌剧厅",
                "portraitKey": "江苏",
                "portraitTitle": "客源分布",
                "portraitValue": "148.00",
                "ratio": "3.86"
            },
            {
                "poiCode": "20201003",
                "poiName": "三福百货",
                "portraitKey": "湖北",
                "portraitTitle": "客源分布",
                "portraitValue": "147.00",
                "ratio": "3.83"
            }
        ]

jdk8的stream还支持groupby,对list的某个属性进行分组可以用于统计某个属性总共出现多少次,和一个有多少个属性值出现。用法如下:

if(!CollectionUtils.isEmpty(contingencyDeviceIDTOList)){
    Map collectFileId = contingencyDeviceIDTOList.stream().collect(Collectors.groupingBy(doc -> doc.getCmdFileId(), Collectors.counting()));
    if(Objects.nonNull(collectFileId) && collectFileId.entrySet().size()>1){
        throw new RCException("同一批设备只能选一个音频!");
    }
}

你可能感兴趣的:(java)