栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java8 新特性 stream 操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java8 新特性 stream 操作

stream 操作锦集

文章中方法适用于 Java 1.8 及以上版本

排序
//按年龄排序
List collect = list.stream()
    .sorted((e1, e2) -> {
        //先按年龄排序
        return e1.getAge().compareTo(e2.getAge());
    })
    .collect(Collectors.toList());
筛选
List collect = list.stream()
   //选出年龄大于35岁的
   .filter((e) -> e.getAge() > 35)
   //只要前2个
   .limit(2)
   .collect(Collectors.toList());
去重
List hospitalDepartmentList = hospitalWardVos.stream()
	.collect(Collectors.collectingAndThen(
		 Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
				HospitalWardVo::getWardAttributesId))), ArrayList::new)
        );
多字段去重
  List departmentClasss = result.stream().collect(
  		Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
                   o -> o.getHospitalId() + ";" + o.getStandardDepartmentClassId()
                ))), ArrayList::new));
List 中单独返回某个属性值
List wardIds = userWardDepartments.stream().map(
	UserWardDepartment::getId).collect(Collectors.toList()
);
List 返回新对象
List childNode = hospitalDepartmentArr.stream().map((hospitalDepartmentObj) -> {
   Map hospitalDepartmentMap = new HashMap<>(16);
    hospitalDepartmentMap.put("id", hospitalDepartmentObj.getId());
    hospitalDepartmentMap.put("showId", hospitalDepartmentObj.getId());
    hospitalDepartmentMap.put("showName", hospitalDepartmentObj.getWardAttributesName());
    hospitalDepartmentMap.put("level", "2");
    hospitalDepartmentMap.put("pid", item.getWardAttributesId() + "_1");
    return hospitalDepartmentMap;
}).collect(Collectors.toList());
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号