用于集合上的批量数据操作的Java Stream API

示例代码

  1. //从 Collection 和数组
  2. List<Integerlist = new ArrayList<>();
  3. for(int i=0;i<100;i++) {
  4. list.add(i);
  5. }
  6. Stream<Integerstream = list.stream(); //串行流
  7. Stream<Integerstream1 = list.parallelStream(); //并行流
  8. Stream<Integerstream2 = Arrays.stream(list.toArray(new Integer[0]));
  9. Stream<Integerstream3 = Stream.of(list.toArray(new Integer[0]));
  10. //从 BufferedReader
  11. BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("path")));
  12. Stream<Stringstream4 = bufferedReader.lines();
  13. //静态工厂
  14. IntStream stream5 = IntStream.rangeClosed(1, 100);//生成1-100 的int stream
  15. Stream<Pathstream6 = Files.walk(Paths.get("path"), 100);
  16. //自己构建 通过StreamSupport辅助类从spliterator产生流
  17. Stream<Integerstream7 = StreamSupport.stream(list.spliterator(), false);
  18. //其它
  19. Random random = new Random();
  20. IntStream stream8 = random.ints();
  21. BitSet bitSet = BitSet.valueOf(new long[]{1L, 2L, 3L});
  22. IntStream stream9 = bitSet.stream();
  23. Pattern pattern = Pattern.compile("\\d+");
  24. Stream<Stringstream10 = pattern.splitAsStream("111sda123sda");
  25. JarFile jarFile = new JarFile("xxx.jar");
  26. Stream<JarEntrystream11 = jarFile.stream();