用于集合上的批量数据操作的Java Stream API
示例代码
//从 Collection 和数组
List<Integerlist = new ArrayList<>();
for(int i=0;i<100;i++) {
list.add(i);
}
Stream<Integerstream = list.stream(); //串行流
Stream<Integerstream1 = list.parallelStream(); //并行流
Stream<Integerstream2 = Arrays.stream(list.toArray(new Integer[0]));
Stream<Integerstream3 = Stream.of(list.toArray(new Integer[0]));
//从 BufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("path")));
Stream<Stringstream4 = bufferedReader.lines();
//静态工厂
IntStream stream5 = IntStream.rangeClosed(1, 100);//生成1-100 的int stream
Stream<Pathstream6 = Files.walk(Paths.get("path"), 100);
//自己构建 通过StreamSupport辅助类从spliterator产生流
Stream<Integerstream7 = StreamSupport.stream(list.spliterator(), false);
//其它
Random random = new Random();
IntStream stream8 = random.ints();
BitSet bitSet = BitSet.valueOf(new long[]{1L, 2L, 3L});
IntStream stream9 = bitSet.stream();
Pattern pattern = Pattern.compile("\\d+");
Stream<Stringstream10 = pattern.splitAsStream("111sda123sda");
JarFile jarFile = new JarFile("xxx.jar");
Stream<JarEntrystream11 = jarFile.stream();