流的转换会产生一个新流,它的元素派生出自另一个流中的元素;

Stream<T> filter(Predicate<? super T> predicate)   返回一个包含此流中与某种条件相匹配的流

<R> Stream<R> map(Function<? super T,? extends R> mapper)  返回由将给定函数应用于此流的元素的结果组成的流。

<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper) 返回一个流,该流包含将此流的每个元素替换为通过将所提供的映射函数应用于每个元素而生成的映射流的内容的结果。每个映射流在其内容被放置到这个流之后关闭(如果映射流为空,则使用空流)。

Filter转换产生一个新流,它的元素与某种条件相匹配;

  1. 1 String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
  2. 2 List<String> words = Arrays.asList(contents.split("\\PL+"));
  3. 3 //filter转换会产生一个流(值包含长单词的另一个流)
  4. 4 Stream<String> newStream = words.stream()
  5. 5 .filter(w -> w.length() > 6);

在使用map时,会有一个函数应用到每个元素上,并且其结果包含了应用该函数后所产生的所有结果流;

  1. 1 String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
  2. 2 List<String> words = Arrays.asList(contents.split("\\PL+"));
  3. 3 //map按照某种方式来转换流中的值
  4. 4 Stream<String> mapStream = words.stream().map(String::toUpperCase);
  5. 5 List<String> list = mapStream.limit(10)
  6. 6 .collect(Collectors.toList());
  7. 7 for(int i=0;i<list.size();i++){
  8. 8 System.out.println(list.get(i));
  9. 9 }
  1. 1 String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
  2. 2 List<String> words = Arrays.asList(contents.split("\\PL+"));
  3. 3 Stream<String> result = words.stream()
  4. 4 .flatMap(w -> letters(w));
  5. 5 show(result);
  6. 6
  7. 7 public static Stream<String> letters(String s){
  8. 8
  9. 9 ArrayList<String> result = new ArrayList<>();
  10. 10 for(int i=0;i<s.length();i++){
  11. 11 result.add(s.substring(i,i+1));
  12. 12 }
  13. 13 return result.stream();
  14. 14 }
  15. 15
  16. 16 public static <T> void show( Stream<T> stream){
  17. 17 List<T> list = stream.limit(10)
  18. 18 .collect(Collectors.toList());
  19. 19 for(int i=0;i<list.size();i++){
  20. 20
  21. 21 System.out.println(list.get(i));
  22. 22 }
  23. 23 }

Stream<T> limit(long maxSize)    返回由此流的元素组成的流,截断长度不超过maxSize。

Stream<T> skip(long n)               丢弃流的前n个元素之后,返回由该流的其余元素组成的流。 如果这个流包含少于n个元素,那么将返回一个空的流。

static <T> Stream<T> concat(Stream<? extends T> a,Stream<? extends T> b)   创建一个延迟连接的流,其元素是第一个流的所有元素,后跟第二个流的所有元素。 如果两个输入流都是有序的,则生成的流是有序的;如果任意一个输入流是并行的,则生成的流是并行的。 当结果流关闭时,调用两个输入流的关闭处理程序。

  1. 1 /**
  2. 2 * Created by Lenovo on 2017/12/18.
  3. 3 * 抽取子流和链接流
  4. 4 */
  5. 5 public class Demo07 {
  6. 6
  7. 7 private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";
  8. 8
  9. 9 public static void main(String[] args) throws Exception {
  10. 10
  11. 11 String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
  12. 12 List<String> words = Arrays.asList(contents.split("\\PL+"));
  13. 13
  14. 14 //包含有10个随机数的流
  15. 15 Stream<Double> random = Stream.generate(Math::random).limit(10);
  16. 16 show(random);
  17. 17
  18. 18 //split,丢弃前n个元素
  19. 19 Stream<String> skip = words.stream().skip(5);
  20. 20 show(skip);
  21. 21
  22. 22 //concat将两个流连接起来
  23. 23 Stream<String> concat = Stream.concat(letters("Hello"), letters("World"));
  24. 24 show(concat);
  25. 25 }
  26. 26 public static <T> void show( Stream<T> stream){
  27. 27 List<T> list = stream.limit(10)
  28. 28 .collect(Collectors.toList());
  29. 29 for(int i=0;i<list.size();i++){
  30. 30
  31. 31 System.out.println(list.get(i));
  32. 32 }
  33. 33 }
  34. 34 public static Stream<String> letters(String s){
  35. 35
  36. 36 ArrayList<String> result = new ArrayList<>();
  37. 37 for(int i=0;i<s.length();i++){
  38. 38 result.add(s.substring(i,i+1));
  39. 39 }
  40. 40 return result.stream();
  41. 41 }
  42. 42 }

Stream<T> distinct()       返回由此流的不同元素(根据Object.equals(Object))组成的流。

Stream<T> sorted()        返回由此流的元素组成的流,按照自然顺序排序。如果此流的元素不是Comparable,执行终端操作时可能会抛出java.lang.ClassCastException。

Stream<T> peek(Consumer<? super T> action)   返回由此流的元素组成的流,另外对每个元素执行提供的操作,因为元素将从结果流中消耗。这是一个中间操作。

  1. 1 /**
  2. 2 * Created by Lenovo on 2017/12/18.
  3. 3 * 其他转换流
  4. 4 */
  5. 5 public class Demo06 {
  6. 6
  7. 7 public static void main(String[] args) {
  8. 8
  9. 9 //将原有的流去重,获取一个新流
  10. 10 Stream<String> distinct = Stream.of("aaa", "bbb", "ccc", "sss", "aaa").distinct();
  11. 11 show(distinct);
  12. 12
  13. 13 //sorted,倒序排序
  14. 14 Stream<String> sorted = Stream.of("aaa", "aa", "aaaa", "a", "aaaaa")
  15. 15 .sorted(Comparator.comparing(String::length).reversed());
  16. 16 show(sorted);
  17. 17
  18. 18 //peek
  19. 19 Object[] peek = Stream.iterate(1.0, p -> p * 2)
  20. 20 .peek(e -> System.out.println("fetching" + e))
  21. 21 .limit(20).toArray();
  22. 22 for(int i = 0;i<peek.length;i++){
  23. 23 System.out.println(peek[i]);
  24. 24 }
  25. 25 }
  26. 26
  27. 27 public static <T> void show(Stream<T> stream){
  28. 28 List<T> tList = stream.limit(10).collect(Collectors.toList());
  29. 29 for(int i =0;i<tList.size();i++){
  30. 30 System.out.println(tList.get(i));
  31. 31 }
  32. 32 }
  33. 33 }

结果输出:

  1. 1 aaa
  2. 2 bbb
  3. 3 ccc
  4. 4 sss
  5. 5 aaaaa
  6. 6 aaaa
  7. 7 aaa
  8. 8 aa
  9. 9 a
  10. 10 fetching1.0
  11. 11 fetching2.0
  12. 12 fetching4.0
  13. 13 fetching8.0
  14. 14 fetching16.0
  15. 15 fetching32.0
  16. 16 fetching64.0
  17. 17 fetching128.0
  18. 18 fetching256.0
  19. 19 fetching512.0
  20. 20 fetching1024.0
  21. 21 fetching2048.0
  22. 22 fetching4096.0
  23. 23 fetching8192.0
  24. 24 fetching16384.0
  25. 25 fetching32768.0
  26. 26 fetching65536.0
  27. 27 fetching131072.0
  28. 28 fetching262144.0
  29. 29 fetching524288.0

 

版权声明:本文为qlwang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/qlwang/p/8060290.html