博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java集合框架 流 Streams| Mapping| Filtering| Slicing | Sorting |Reducing | Collectors|...
阅读量:3967 次
发布时间:2019-05-24

本文共 7118 字,大约阅读时间需要 23 分钟。

Main.java

package Stream;public class Main {
public static void main(String[]argv){
StreamDemo.show(); }}

Movie.java

package Stream;public class Movie implements Comparable
{
private int likes; private String name; public Movie(String name,int likes){
this.name=name; this.likes=likes; } public int getLikes(){
return likes; } @Override public int compareTo(Movie o) {
return this.getLikes()-o.getLikes(); } public String getName() {
return name; }}

StreamDemo.java

package Stream;import java.util.*;import java.util.stream.*;/*Contents:* Streams* Mapping* Filtering* Slicing* Sorting* Reducing* Collectors* ...* */public class StreamDemo {
public static void show(){
List
movies=List.of( new Movie("b",10), new Movie("a",15), new Movie("c",20) ); int count=0; for(var movie:movies){
if(movie.getLikes()>10){
count++; } } System.out.println(count);//output 2 //Declarative(Function) Programming //获得集合的流 var count1=movies.stream().count(); var count2=movies.stream().filter(movie -> {
return movie.getLikes()>10;}).count(); System.out.println(count1);//3 System.out.println(count2);//2 //获得数组流 int[]numbers={
1,2,3,4,5,6,7,8,9}; Arrays.stream(numbers).forEach((int n)->{
System.out.println(n+1);}); /*如何创建流对象*/ var stream=Stream.generate(()->Math.random()); //使用流对象 stream.limit(3).forEach(n->System.out.println(n));//输出三个随机数 //生成有限流 Stream.iterate(1,n->n+1).limit(10).forEach(n->System.out.println(n)); //起始值为1 以后每个元素比前一个元素+1 /*stream map 映射*/ movies.stream().map(movie->{
return movie.getLikes();}).forEach( like->System.out.println(like) ); /*当集合存储集合时使用flatMap*/ var stream1=Stream.of( List.of(1,2,3), List.of(4,5,6)); //Stream
> -> Stream
stream1.flatMap(list->list.stream()) .forEach(list->System.out.println(list)); /*Filtering Elements*/ movies.stream() .filter(movie -> movie.getLikes()>10). forEach(movie -> {
System.out.println("filter--"+movie.getLikes());}); /*stream的方法分为两种 * Intermediate(产生新的流通道) Terminal(不产生新的流通道) * map forEach * flatMap * filter * */ /*Slicing a Stream*/ //limit(n) skip(n) takeWhile(predicate) dropWhile(predicate) /*limit(n)*/ movies.stream().limit(2) .forEach(movie->System.out.println("limit--"+movie.getLikes())); /*skip(n)*/ movies.stream().skip(2) .forEach(movie -> System.out.println("movie--"+movie.getLikes())); /*takeWhile(Predicate)*/ movies.stream().takeWhile(movie -> movie.getLikes()!=15) .forEach(movie -> System.out.println("takeWhile--"+movie.getLikes())); /*Sorting Stream*/ //sorting comparable interface movies.stream().sorted().forEach(movie -> {
System.out.println(movie.getLikes());}); //sorting comparator movies.stream().sorted((a,b)->a.getName().compareTo(b.getName())) .forEach(movie -> System.out.println("comparator--"+movie.getName())); //sorting comparator comparing movies.stream().sorted(Comparator.comparing(movie->movie.getName())) //movies.stream().sorted(Comparator.comparing(Movie::getName)) .forEach(movie -> System.out.println("comparator.comparing--"+movie.getName())); //倒序排序 movies.stream().sorted(Comparator.comparing(Movie::getName).reversed()) //movies.stream().sorted(Comparator.comparing(Movie::getName)) .forEach(movie -> System.out.println("comparator.comparing reversed--"+movie.getName())); //Getting Distinct Elements List
movies1=List.of( new Movie("d",10), new Movie("b",10), new Movie("a",15), new Movie("c",20) ); movies1.stream() .map(Movie::getLikes) .distinct() .forEach(System.out::println); /*10 15 20*/ //Peeking Elements 中途去干些什么 movies.stream().filter(m->m.getLikes()>10) .peek(movie -> System.out.println("filter later--"+movie.getLikes())) .map(Movie::getName) .peek(Name->System.out.println("map Name later--"+Name)) .forEach(System.out::println); /*a c*/ /*REDUCERS*/ /*count() * anyMatch(Predicate) * allMatch(Predicate) * noneMatch(Predicate) * findFirst() * findAny() * max(comparator) * min(comparator) * */ List
movies2=List.of( new Movie("d",10), new Movie("b",10), new Movie("a",15), new Movie("c",20) ); //统计全部数量 movies2.stream().count(); //统计满足条件的数量 movies2.stream().anyMatch(m->m.getLikes()>10); //检测所有是否都满条件 movies2.stream().allMatch(m->m.getLikes()>10); //检测是否没有元素满足条件 movies2.stream().noneMatch(m->m.getLikes()>10); //max movies2.stream().max( Comparator.comparing(Movie::getName) ).get().getName(); //min movies2.stream().min( Comparator.comparing(Movie::getName) ).get().getName(); //reduce Optional
sum=movies2.stream().map(m->m.getLikes()) .reduce((a,b)->a+b); System.out.println(sum.get());//当没有值时将会返回null System.out.println(sum.orElse(0));//当没有值时将会返回0 //Collectors List
over10movies=movies2.stream() .filter(m->m.getLikes()>10) .collect(Collectors.toList()); Map
mapMovies=movies2.stream() .collect(Collectors.toMap(m->m.getName(),m->m.getLikes())); Double summarizingdouble=movies2.stream().collect( Collectors.summingDouble(m->m.getLikes()) ); System.out.println(summarizingdouble); String summarizingstring=movies2.stream() .map(m->m.getName()) .collect( Collectors.joining(",") );//d,b,a,c System.out.println(summarizingstring); //Grouping Elements 分组 var GroupingResult=movies2.stream() .collect(Collectors.groupingBy(Movie::getLikes)); // Type of GroupingResult: Map
> /* movies2.stream() .collect(Collectors.groupingBy(Movie::getLikes,Collector.toSet())); Type of GroupingResult: Map
> */ var resultString=movies2.stream() .collect(Collectors.groupingBy(Movie::getLikes,Collectors.mapping( Movie::getName,Collectors.joining(",") ))); /*resultString Type Map
*/ /*Partitioning Elements 划分元素*/ var result11=movies2.stream().collect( Collectors.partitioningBy(m->m.getLikes()>20) ); /*result11 type: Map
>*/ var result22=movies2.stream().collect( Collectors.partitioningBy(m->m.getLikes()>20, Collectors.mapping(Movie::getName, Collectors.joining(","))) ); System.out.println(result22);//{false=d,b,a,c, true=} //Primitive Type Streams IntStream.of(1,2,3); DoubleStream.of(1,4.4,23.4); LongStream.of(3,6,4,23); IntStream.range(1,3).forEach(System.out::println);//1 2 }}

转载地址:http://gucki.baihongyu.com/

你可能感兴趣的文章
DB2 物化查询表
查看>>
IF 语句
查看>>
循环语句
查看>>
DB2 临时表
查看>>
ITERATE、LEAVE、GOTO和RETURN
查看>>
异常处理
查看>>
存储过程
查看>>
动态SQL(Dynamic SQL)
查看>>
在存储过程之间传递数据
查看>>
迁移存储过程
查看>>
GET DIAGNOSTIC 语句
查看>>
Python 简介
查看>>
Python 注释
查看>>
Python 变量
查看>>
Python 数据类型 -- 数字
查看>>
Spring 管理对象
查看>>
Spring 自定义对象初始化及销毁
查看>>
Spring Batch 环境设置
查看>>
字符组转译序列
查看>>
字符转译序列
查看>>