1.介绍:
Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk data operation)。Stream API 借助于同样新出现的 Lambda 表达式,极大的提高编程效率和程序可读性。
2.原理:
这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。
集合有两种方式生成流:
中间操作(intermediate)主要有以下方法(此类型的方法返回的都是Stream对象):
map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
终端操作(terminal)主要有以下方法:
forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
代码如下:
package cn.tedu;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Stream {
static final Logger logger = LoggerFactory.getLogger(Stream.class);
public static void main(String[] args) {
List list = Arrays.asList("aaa", "ddd", "bbb", "ccc", "a2a", "d2d", "b2b", "c2c", "a3a", "d3d", "b3b", "c3c");
List list1 = Arrays.asList(1, 2);
//filter过滤条件
list.stream()
.filter((s) -> s.contains("a"))
.forEach(s -> System.out.println(s));
list.stream()
.filter((s) -> s.contains("b"))
.forEach(s -> System.out.println(s));
//map添加条件
list.stream()
.filter((s) -> s.contains("a"))
.map((s) -> s + "--kkk")
.forEach(s -> System.out.println(s));
list.stream()
.filter((s) -> s.contains("a"))
.mapToInt((s) -> s.hashCode())
.forEach(s -> System.out.println(s));
List collect = list1.stream()
.filter(s -> s > 1).collect(Collectors.toList());
System.out.println(collect);
/*if(!collect.isEmpty()){
return ;
}*/
List setList = new ArrayList<>();
setList.add(new String[]{"aa", "bb"});
setList.add(new String[]{"cc", "dd"});
setList.add(new String[]{"ee", "ff"});
//使用map方法
System.out.println(setList.get(1));
logger.warn(String.valueOf(setList));
setList.stream()
.map(s -> Arrays.stream(s))
.forEach(s -> System.out.println("map==" + s));
//使用flatMap方法
setList.stream()
.flatMap(s -> Arrays.stream(s))
.forEach(s -> System.out.println("flatMap==" + s));
//distinct:将集合中的元素去重。
List disList = Arrays.asList("aaa", "ddd", "bbb", "ddd", "aaa");
disList.stream()
.distinct()
.forEach(s -> System.out.println(s));
//sorted:将集合中的元素排序。
List integerList = Arrays.asList(5, 2, 3, 4);
integerList.stream()
.sorted()
.forEach(s -> System.out.println(s));
integerList.stream()
.sorted((s1, s2) -> s2.compareTo(s1))
.forEach(s -> System.out.println(s));
//peek中间件操作 forEach终端操作
integerList.stream()
.peek(s -> System.out.println("peek = " + s)).collect(Collectors.toList())
;
integerList.stream()
.peek(s -> System.out.println("peek = " + s))
.forEach(s -> System.out.println("forEach = " + s));
;
List collects = integerList.stream()
.filter(a -> a > 2)
.collect(Collectors.toList());
System.out.println(collects);
System.out.println("99999999999999999999999");
//limit:返回Stream的前n个元素。
integerList.stream()
.limit(2)
.forEach(s -> System.out.println(s));
//skip:删除Stream的前n个元素。
integerList.stream()
.skip(1)
.forEach(s -> System.out.println(s));
//parallelStream并行流 forEach是乱序 forEachOrdered是有序
integerList.parallelStream()
.forEach(s -> System.out.println(s));
integerList.parallelStream()
.forEachOrdered(s -> System.out.println(s));
//min:获取集合中最小值。
Integer min = integerList.stream()
.filter(a -> a > 1)
.min((Integer a, Integer b) -> a.compareTo(b))
.get();
System.out.println(min);
//max:获取集合中最大值。
Integer max = integerList.stream()
.filter(a -> a > 1)
.max((Integer a, Integer b) -> a.compareTo(b))
.get();
System.out.println(max);
//count:获取集合中元素个数
long count = integerList.stream()
.filter(a -> a > 1)
.count();
System.out.println(count);
//anyMatch: Stream 中任意一个元素符合传入的 predicate,返回 true
//allMatch:Stream 中全部元素符合传入的 predicate,返回 true
//noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true
boolean b = integerList.stream()
.anyMatch(s -> s > 0);
boolean b1 = integerList.stream()
.allMatch(s -> s > 0);
boolean b2 = integerList.stream()
.noneMatch(s -> s > 0);
System.out.println("anyMatch = " + b);
System.out.println("allMatch = " + b1);
System.out.println("noneMatch = " + b2);
//
}
}
控制台输出结果如下:
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar=54062:C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\java\CGB05\springcloud1\MyModule\out\production\MyModule" cn.tedu.Stream
aaa
a2a
a3a
bbb
b2b
b3b
aaa--kkk
a2a--kkk
a3a--kkk
96321
94864
94895
[2]
[Ljava.lang.String;@1d81eb93
八月 03, 2022 10:39:10 上午 com.sun.org.slf4j.internal.Logger warn
警告: [[Ljava.lang.String;@7291c18f, [Ljava.lang.String;@1d81eb93, [Ljava.lang.String;@34a245ab]
map==java.util.stream.ReferencePipeline$Head@1b2c6ec2
map==java.util.stream.ReferencePipeline$Head@4edde6e5
map==java.util.stream.ReferencePipeline$Head@70177ecd
flatMap==aa
flatMap==bb
flatMap==cc
flatMap==dd
flatMap==ee
flatMap==ff
aaa
ddd
bbb
2
3
4
5
5
4
3
2
peek = 5
peek = 2
peek = 3
peek = 4
peek = 5
forEach = 5
peek = 2
forEach = 2
peek = 3
forEach = 3
peek = 4
forEach = 4
[5, 3, 4]
99999999999999999999999
5
2
2
3
4
3
4
2
5
5
2
3
4
2
5
4
anyMatch = true
allMatch = true
noneMatch = false
Process finished with exit code 0