本文主要是介绍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流常见方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!