当前位置: 首页 > news >正文

广告设计网站官网国家信息公示系统

广告设计网站官网,国家信息公示系统,天津众业建设工程有限公司网站,河南建设厅特种工报考网站stream流 1.介绍2.将List转成Set3.将List转成Map4.计算求和reduce5.查找最大值max和最小值min6.Match匹配7.过滤器 filter8.分页limit 跳过skip9.数据排序 sorted 1.介绍 stream流可以非常方便与精简的形式遍历集合,实现过滤、排序等功能 2.将List转成Set stream…

stream流

  • 1.介绍
  • 2.将List转成Set
  • 3.将List转成Map
  • 4.计算求和reduce
  • 5.查找最大值max和最小值min
  • 6.Match匹配
  • 7.过滤器 filter
  • 8.分页limit 跳过skip
  • 9.数据排序 sorted

1.介绍

stream流可以非常方便与精简的形式遍历集合,实现过滤、排序等功能

2.将List转成Set

stream.collect(Collectors.toSet());

public class Test01 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));// 创建stream有两种方式// 1.串行流// 2.并行流// 并行流 比 串行流 效率要高Stream<UserEntity> stream = userEntities.stream();// 转换成set集合Set<UserEntity> userEntitySet = stream.collect(Collectors.toSet());userEntitySet.forEach(userEntity -> {System.out.println(userEntity);});}
}

3.将List转成Map

需要指定谁作为key,谁作为value
stream.collect(Collectors.toMap());

public class Test02 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));// list集合并没有key// 指定userName作为key,user对象作为valueStream<UserEntity> userEntityStream = userEntities.stream();/*** new Function<UserEntity(List集合中的类型), String(Map中的Key)>()*/Map<String, UserEntity> collect = userEntityStream.collect(Collectors.toMap(new Function<UserEntity, String>() {@Overridepublic String apply(UserEntity userEntity) {// 设置key的值return userEntity.getUserName();}}, new Function<UserEntity, UserEntity>() {@Overridepublic UserEntity apply(UserEntity userEntity) {// 设置value的值return userEntity;}}));collect.forEach(new BiConsumer() {@Overridepublic void accept(Object key, Object value) {System.out.println(key + ": " + value);}});}
}

4.计算求和reduce

public class Test03 {public static void main(String[] args) {Stream<Integer> integerStream = Stream.of(10, 89, 204, 56);Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {@Overridepublic Integer apply(Integer integer, Integer integer2) {return integer + integer2;}});System.out.println(reduce.get());System.out.println("================================================");// 计算每个user的年龄的总数ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));Stream<UserEntity> userEntityStream = userEntities.stream();Optional<UserEntity> sum = userEntityStream.reduce(new BinaryOperator<UserEntity>() {@Overridepublic UserEntity apply(UserEntity u1, UserEntity u2) {return new UserEntity("sum", u1.getAge() + u2.getAge());}});System.out.println(sum.get().getAge());}
}

5.查找最大值max和最小值min

public class Test04 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));// 最大值Stream<UserEntity> userEntityStream1 = userEntities.stream();Optional<UserEntity> max = userEntityStream1.max((o1, o2) -> o1.getAge() - o2.getAge());System.out.println("max = " + max.get());// 最小值Stream<UserEntity> userEntityStream2 = userEntities.stream();Optional<UserEntity> min = userEntityStream2.min((o1, o2) -> o1.getAge() - o2.getAge());System.out.println("min = " + min.get());}
}

6.Match匹配

anyMatch:判断条件里,任意一个元素成功,返回true
AllMatch:判断条件里,所有的都是成功,返回true
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true

public class Test05 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",16));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 12));Stream<UserEntity> userEntityStream = userEntities.stream();boolean b = userEntityStream.noneMatch(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "zhangsan".equals(userEntity.getUserName());}});System.out.println("b = " + b);}
}

7.过滤器 filter

public class Test06 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));Stream<UserEntity> userEntityStream = userEntities.stream();userEntityStream.filter(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "zhaoliu".equals(userEntity.getUserName()) && userEntity.getAge() > 2;}}).forEach(userEntity -> System.out.println(userEntity));}
}

8.分页limit 跳过skip

public class Test07 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));// 只查询前5条Stream<UserEntity> userEntityStream1 = userEntities.stream();userEntityStream1.limit(3).forEach(userEntity -> System.out.println(userEntity));System.out.println("====================");// 查询 第2条-4条Stream<UserEntity> userEntityStream2 = userEntities.stream();userEntityStream2.skip(2).limit(2).forEach(userEntity -> System.out.println(userEntity));}
}

9.数据排序 sorted

public class Test08 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));Stream<UserEntity> userEntityStream = userEntities.stream();userEntityStream.sorted(new Comparator<UserEntity>() {@Overridepublic int compare(UserEntity o1, UserEntity o2) {return o2.getAge() - o1.getAge();}}).forEach(userEntity -> System.out.println(userEntity));}
}
http://www.yayakq.cn/news/563392/

相关文章:

  • 高端建站神器策划公司职位
  • php做外贸网站好吗济南网站建设济南
  • 做搜狗手机网站优500人在线网站建设配置
  • 织梦网站转跳手机站创建官方网站网址
  • 泗县建设银行网站网站建设面试试题
  • 佛山智唯网站建设wordpress音乐站源码
  • 网站定制公司kinglink网站备案信息保护
  • 西宁摄网站制作营销网站建设企业
  • 设计公司网站公司详情wordpress 二维码登录
  • 网站域名 空间申请网站 标题 关键词 描述
  • 做企业网站需要建多大的画布wordpress最大上传大小怎么改
  • 搭建免费个人网站2022大连网站建设资讯
  • 建网站需要编程吗最新网站建设进度表
  • 数字中国建设峰会 官方网站crm系统是干什么的
  • 网站开发与设计的参考文献asp.net网站开发文档
  • 自己做游戏app的网站电商平台首页设计
  • 公司网站设计要多少钱晋城市住房城乡建设局网站
  • 重庆工业设计公司有哪些seo排名大概多少钱
  • 钟表商城网站建设方案唐山建设工程安全监督网站
  • 找家里做的工作上哪个网站无锡 电子商务网站建设
  • 杭州营销型网站建设排名嘉兴网站建设定制网站
  • html5快速建站设计公司企业标志
  • 国外哪些做问卷赚钱的网站seo网站是什么意思
  • 公司 网站 源码什么网站的页面好看
  • 重庆建网站公司wordpress适配手机端
  • 企业官网网站网站建设除了中企动力
  • 如何创建自己公司的网站WordPress做推广
  • 奎屯网站建设网站优化 毕业设计
  • 网站设计制作程序wordpress 仿微博模板
  • 自己做公司网站难吗e4a怎么做点击跳转网站