First experience of Stream
Stream is an important feature of operation collections in Java 8. Let’s first look at how Stream is defined in Java:
"A sequence of elements supporting sequential and parallel aggregate operations."
Let's interpret the above sentence:
1. Stream is a collection of elements, which makes Stream look like iterator;
2. It can support the operations of aggregating the original Stream in sequence and parallel.
There are many ways to create Stream, and there are several other ways to create a collection in addition to the most common ones.
List to Stream
List inherits from the Collection interface, and Collection provides the stream() method.
List<Integer> list = Lists.newArrayList(1, 2, 3);Stream<Integer> stream = list.stream();
Array to stream
For arrays, Arrays provides the stream() method.
String[] arr = new String[]{"a", "b", "c"};Stream<String> stream = Arrays.stream(arr); Map to stream
Map is not a sequence, not a collection, and there is no way to convert it directly into stream(). But entrySet() is Set, and it can be converted.
Map<String, Object> map = Maps.newHashMap();Stream<Entry<String, Object>> stream = map.entrySet().stream();
Create Stream directly
Stream also provides an API to directly generate a Stream, which can be roughly understood as a List. Because the internal is implemented by an array.
Stream<Integer> integerStream = Stream.of(1, 2, 3);
Read the Stream of the file
Those who have used Linux will admire the pipe symbols on the command line, and a pipe symbol can be processed continuously. Similar functions can be implemented when reading files in Java.
long uniqueWords = 0;try (Stream<String> lines = Files.lines(Paths.get("data.txt"), Charset.defaultCharset())) { uniqueWords = lines.flatMap(l -> Arrays.stream(l.split(")))) .distinct() .count();} catch (IOException e) { //} Generate infinite flow through functions
Stream provides iterate to generate an infinite sequence, an infinite sequence based on the initial value. You can use lambda to set the sequence generation rules, such as adding 2 each time.
Stream.iterate(0, n -> n + 2) .limit(10) .forEach(System.out::println);
For example, Fibonacci sequence
Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}) .limit(20) .map(t -> t[0]) .forEach(System.out::println); Stream also provides another generate method to generate sequences. Receives a user-specified generated sequence function IntSupplier.
IntSupplier fib = new IntSupplier() { private int previous = 0; private int current = 1; @Override public int getAsInt() { int oldPrevious = this.previous; int nextValue = this.previous + this.current; this.previous = this.current; this.current = nextValue; return oldPrevious; }};IntStream.generate(fib).limit(10).forEach(System.out::println);Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.