流的转换, 按照条件过滤/映射/摊平/截取/丢弃/连接/去重/排序。

辅助方法

public static int myCompare(String x, String y) {
if(x.length()>y.length()){
return 1;
}else if(x.length()==y.length()){
return x.compareTo(y);
}else{
return -1;
}
} private static Stream<Integer> letters(String s){
return s.chars().boxed(); //将所有的chars抽成流
} public static <T> void show(String title, Stream<T> stream){
System.out.println("title:"+title); List<T> collect = stream.limit(10).collect(Collectors.toList());
collect.forEach(n->System.out.println(n));
System.out.println();
} //????
@SuppressWarnings("unchecked")
public static <T extends Stream<?>> void deepShow(String title, T stream){
System.out.println("title:"+title); stream.forEach(n->{
if(n instanceof Stream){
deepShow(title, (T)n);
}else{
System.out.println(n);
}
}); System.out.println();
}

1. 过滤 filter()  Predicate<? super T> predicate  T->boolean   按照特定条件过滤

Stream<String> filterStream = arrayList.stream().filter((w)->(w.length()>2));
show("filterStream", filterStream);

2. 映射  map()   Function<? super T, ? extends R> mapper  T->R  按照特定方式转换

Stream<String> mapStream = arrayList.stream().map(String::toUpperCase);
show("mapStream", mapStream);

3. 摊平 flatMap()  包含流的流  摊平

Stream<Stream<Integer>> flatStream = arrayList.stream().map(w->letters(w));
deepShow("flatStream", flatStream); //==>摊平
Stream<Integer> flatMapStream = arrayList.stream().flatMap(w->letters(w));
flatMapStream.forEach(n->System.out.println((char)(int)n));
System.out.println();

4. 截取 limit(n)  在n个元素后结束

Stream<String> limitStream = arrayList.stream().limit(2);
show("limitStream", limitStream);

5. 丢弃 skip(n)   丢弃前n个元素

Stream<String> skipStream = arrayList.stream().skip(2);
show("skipStream", skipStream);

6. 连接 Stream.concat()  将两个流连接起来,第一个流不能是无限流,否则第二个流没有处理的机会

Stream<String> concatStream = Stream.concat(arrayList.stream(), Stream.of("aa","bb","cc"));
show("concatStream", concatStream);

7. 去重 distinct()   剔除重复元素

Stream<String> distinctStream = Stream.of("aa","bb","cc","aa").distinct();
show("distinctStream", distinctStream);

8. 排序 sorted()   按照默认排序或者传入比较器

//(1) 按照默认排序, 字典顺序比较
Stream<String> sortedStream = Stream.of("aa","bb","cc","aa").sorted();
show("sortedStream", sortedStream); sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(x->x)); //提取Comparator进行排序
show("sortedStream", sortedStream); //(2) 传入String的比较器
//1) 字典顺序比较
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(String::compareTo);
show("sortedStream", sortedStream); sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted((x,y)->x.compareTo(y));
show("sortedStream", sortedStream); //2) 比较length
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length)); //提取Comparator进行排序
show("sortedStream", sortedStream); sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted((x,y)->Integer.compare(x.length(), y.length()));
show("sortedStream", sortedStream); //3) 传入比较器 比较多个条件
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(
(x, y)->{
if(x.length()>y.length()){
return 1;
}else if(x.length()==y.length()){
return x.compareTo(y); //字典顺序比较
}else{
return -1;
}
});
show("sortedStream", sortedStream); sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(new Comparator<String>() {
@Override
public int compare(String x, String y) {
if(x.length()>y.length()){
return 1;
}else if(x.length()==y.length()){
return x.compareTo(y);
}else{
return -1;
}
}
});
show("sortedStream", sortedStream); //4) 传入自定义的比较器 比较多个条件
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(ConvertStreamTest::myCompare);
show("sortedStream", sortedStream); //5) 语法糖 比较多个条件
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length).thenComparing(String::compareTo));
show("sortedStream", sortedStream); //(3) reversed() 指定比较器倒叙
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing((String x)->x).reversed());
show("sortedStream", sortedStream); sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length).reversed());
show("sortedStream", sortedStream);

9. 类似代理 peek()   获得每个元素时,做一些事情

Stream<String> peekStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").peek(x->System.out.println("peek() fetch element: "+x));
show("peekStream", peekStream);

最新文章

  1. 2016ACM/ICPC亚洲区沈阳站-重现赛
  2. 如何成为python高手
  3. 学习 Linux,101: Linux 命令行
  4. angularjs指令(二)
  5. bigworld源码分析(2)—— loginApp分析
  6. 实现跨域的N种方法
  7. WordPress 主题框架是如何工作的
  8. Win32下 Qt与Lua交互使用(三):在Lua脚本中connect Qt 对象
  9. office文件密码破解方法及软件
  10. ThinkPHP 3.1.2 URL&lt;1&gt;
  11. 最新版-MySQL8.0 安装 - 改密码 之坑
  12. python学习03
  13. 强化学习-时序差分算法(TD)和SARAS法
  14. Java基础教程(21)--泛型
  15. 机器学习中正则惩罚项L0/L1/L2范数详解
  16. install ros-indigo-tf2
  17. cookie,session,token
  18. C++STL:流迭代器
  19. C# Winform 窗体传值 利用委托 子窗体传值给父窗体
  20. Linux一键脚本合集vps

热门文章

  1. Java-学习-喜欢-品牌:互联网公司成为动物园,拟人化品牌形象真的那么有意思?
  2. 学习笔记之NumPy
  3. Zabbix 课程大纲
  4. USB-IF协会公布最新PD3.0(PPS)协议认证芯片和产品名单
  5. OpenJudge 兔子与樱花
  6. 使用gulp压缩js详细步骤笔记
  7. delphi中Application.MessageBox函数用法详解
  8. MapReduce Demo
  9. RISC与CISCCPU构架
  10. tips:Java中的switch的选择因子