Array
int[] anArray = new int[10];
String[] stringArray = new String[] {"a", "b", "c" };
List
List<String> stringList = java.util.Arrays.asList("a", "b", "c");
List<String> stringList = java.util.Arrays.asList(array);
Array --> List
List<String> list = java.util.Arrays.asList(array);
List --> Array
String[] xx = list.toArray(new String[0]);
Array --> Stream
Stream<String> stream = java.util.Arrays.stream(array);
Stream --> Array
String[] array = stream.toArray(String[]::new);
List --> Stream
Stream<String> stream = list.stream();
Stream --> List
List<String> list = stream.collect(Collectors.toList());
List<String> list = stream.toList(); // >= java 16
Set --> Stream
Stream<String> stream = set.stream();
Stream --> Set
Set<String> set = stream.collect(toSet());
Iterate stream
stream.forEach(System.out::println);
Create Stream
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.*;
Stream<String> stream = Stream.of("a"); >= java 1.9
Stream<String> stream = List.of("a", "b", "c"); >= java 1.9
Stream<String> stream = Stream.of(array); >= java 1.9
Stream<String> stream = Stream.<String>builder().add("a").add("b").build();
Stream<String> stream = Stream.generate(() -> "element").limit(8);
Stream<Integer> stream = Stream.iterate(40, n -> n + 2).limit(20);
IntStream stream = IntStream.range(1, 3);
LongStream stream = LongStream.rangeClosed(1, 3);
DoubleStream stream = DoubleStream.of(1.0, 2.0);
Stream<String> stream = Pattern.compile(", ").splitAsStream("a, b, c");
Path path = Paths.get("c:\\file.txt");
Stream<String> stream = Files.lines(path);
Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8);
Stream --> XXXStream
IntStream stream = Stream.of(1, 2, 3).mapToInt(Integer::intValue);
Stream --> parallel stream
Stream<String> stream = java.util.Arrays.asList("A", "B", "C", "D", "E").parallelStream();
IntStream stream = java.util.stream.IntStream.rangeClosed(1, 10).parallel();
Idiom
String string = Stream.of("apple", "take", "quiver")
.map(String::toUpperCase)
.collect(Collectors.joining(", ", "[", "]"));
// output: ["APPLE", "TAKE", "QUIVER"]
private static Stream<String> streamOf(List<String> list){
return list == null || list.isEmpty() ? Stream.empty() : list.stream();
}
标签:java,Stream,stream,--,List,util
From: https://www.cnblogs.com/alfredxing/p/17997989/stream