数据:参照 https://www.jianshu.com/p/d1ce1e104f49
需求按照 鉴定等级降序升序
自定义比较器
public void streamSort(){
List<DecrepitHouseInfo> addressList = data.toJavaList(DecrepitHouseInfo.class).stream().sorted(new Comparator<DecrepitHouseInfo>() {
@Override
public int compare(DecrepitHouseInfo o1, DecrepitHouseInfo o2) {
return o1.getType().compareTo(o2.getType());
}
}).collect(Collectors.toList());
System.out.println(JSON.toJSON(addressList));
}
lamda
@Test
public void streamSort2(){
List<DecrepitHouseInfo> addressList = data.toJavaList(DecrepitHouseInfo.class).stream().sorted((o1,o2)->{
return o1.getType().compareTo(o2.getType());
}).collect(Collectors.toList());
System.out.println(JSON.toJSON(addressList));
}
内置比较器
public void streamSort3(){
List<DecrepitHouseInfo> addressList = data.toJavaList(DecrepitHouseInfo.class).stream()
//.sorted(Comparator.comparing(DecrepitHouseInfo::getType).reversed())
.sorted(Comparator.comparing(DecrepitHouseInfo::getType))
.collect(Collectors.toList());
System.out.println(JSON.toJSON(addressList));
}
结果:
[{"area":"杭州市淳安县","type2":"泥木结构","city":"杭州市淳安县","street":"威坪镇西山村","type":"C","desc":"农村危旧房信息"},
{"area":"金华市婺城区","type2":"砖木结构","city":"金华市婺城区","street":"白龙桥镇虹路","type":"C","desc":"农村危旧房信息"},
{"area":"温州市乐清市","type2":"砖混结构","city":"温州市乐清市","street":"天成街道巨光村","type":"C","desc":"农村危旧房信息"},
{"area":"丽水市缙云县","type2":"泥木结构","city":"丽水市缙云县","street":"新建镇新建村","type":"C","desc":"农村危旧房信息"},
{"area":"衢州市柯城区","type2":"其他","city":"衢州市柯城区","street":"石梁镇静岩村","type":"C","desc":"农村危旧房信息"},
{"area":"温州市龙港市","type2":"木结构","city":"温州市龙港市","street":"江山片区龙源社区","type":"C","desc":"农村危旧房信息"},
{"area":"金华市武义县","type2":"木结构","city":"金华市武义县","street":"柳城镇东西村","type":"C","desc":"农村危旧房信息"},
{"area":"衢州市常山县","type2":"砖木结构","city":"衢州市常山县","street":"紫港街道孔家弄村","type":"C","desc":"农村危旧房信息"},
{"area":"金华市磐安县","type2":"泥木结构","city":"金华市磐安县","street":"安文街道卢坎头村","type":"C","desc":"农村危旧房信息"},{"area":"衢州市开化县","type2":"砖木结构","city":"衢州市开化县","street":"马金镇徐塘村","type":"C","desc":"农村危旧房信息"},
{"area":"丽水市龙泉市","type2":"泥木结构","city":"丽水市龙泉市","street":"安仁镇张畈村","type":"C","desc":"农村危旧房信息"},
{"area":"湖州市安吉县","type2":"泥木结构","city":"湖州市安吉县","street":"杭垓镇七管村","type":"C","desc":"农村危旧房信息"},
{"area":"衢州市衢江区","type2":"泥木结构","city":"衢州市衢江区","street":"高家镇林家村","type":"C","desc":"农村危旧房信息"},
{"area":"绍兴市诸暨市","type2":"泥木结构","city":"绍兴市诸暨市","street":"陶朱街道白门下村","type":"D","desc":"农村危旧房信息"},
{"area":"衢州市柯城区","type2":"砖木结构","city":"衢州市柯城区","street":"沟溪乡高山窑村","type":"D","desc":"农村危旧房信息"},
{"area":"绍兴市新昌县","type2":"砖木结构","city":"绍兴市新昌县","street":"小将镇南洲村","type":"D","desc":"农村危旧房信息"},
{"area":"金华市开发区","type2":"泥木结构","city":"金华市开发区","street":"罗埠镇孙家村","type":"D","desc":"农村危旧房信息"},
{"area":"丽水市莲都区","type2":"砖木结构","city":"丽水市缙云县","street":"碧湖镇龙岩村","type":"D","desc":"农村危旧房信息"}]
其他快捷方式
// 1、按长度自然排序,即长度从小到大
Arrays.stream(strArr).sorted(Comparator.comparing(String::length)).forEach(System.out::println);
// 2、按长度倒序,即长度从大到小
Arrays.stream(strArr).sorted(Comparator.comparing(String::length).reversed()).forEach(System.out::println);
// 3、首字母倒序
Arrays.stream(strArr).sorted(Comparator.reverseOrder()).forEach(System.out::println);
// 4、首字母自然排序
Arrays.stream(strArr).sorted(Comparator.naturalOrder()).forEach(System.out::println);
总结
对于没有进入stream世界的的人来说,可能stream的语法让人难于理解,但是使用起来确实是变成利器。