Stream流常见方法
2021/10/28 23:17:40
本文主要是介绍Stream流常见方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.ding.stream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.stream.Stream; public class MyStream2 { public static void main(String[] args) { //method1(); //method2(); int [] arr = {1, 2, 3, 4, 5}; Arrays.stream(arr).forEach(s -> System.out.println(s)); Stream.of(1,2,3,4,5).forEach(s -> System.out.println(s)); } private static void method2() { HashMap<String, Integer> hm = new HashMap<>(); hm.put("zhangsan",23); hm.put("lisi",24); hm.put("wangwu",25); hm.put("zhaoliu",26); hm.put("qianqi",27); //双列集合不能直接获取Stream流 hm.keySet().stream().forEach(s -> System.out.println(s)); hm.entrySet().stream().forEach(stringIntegerEntry -> System.out.println(stringIntegerEntry)); } private static void method1() { ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); Stream<String> stream = list.stream(); stream.forEach( s -> System.out.println(s)); list.stream().forEach(s -> System.out.println(s)); } }
package com.ding.stream; import java.util.ArrayList; import java.util.function.Predicate; public class MyStream3 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("张三丰"); list.add("张无忌"); list.add("张翠山"); list.add("王二麻子"); list.add("张良"); list.add("谢广坤"); list.stream().filter( new Predicate<String>() { @Override public boolean test(String s) { boolean starts = s.startsWith("张"); return starts; } } ).forEach(s -> System.out.println(s)); list.stream().filter( (String s) -> { boolean starts = s.startsWith("张"); return starts; } ).forEach(s -> System.out.println(s)); list.stream().filter( s -> s.startsWith("张")).forEach( s -> { System.out.println(s); }); } }
这篇关于Stream流常见方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器
- 2024-11-26Java云原生资料:新手入门教程与实战指南
- 2024-11-26JAVA云原生资料入门教程
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程