An overview of the

The way jQuery is usually written is you select an element and then you do chain programming and then you do a bunch of things. Such as:

$(`.images > img:nth-child(${n}) `)
          .addClass("current")
          .siblings()
          .addClass("enter");          
// Return this at the end of each function to continue the chain
Copy the code

Streams can be played this way, but only on themselves. To set up a production line, different parts of the line body process the original parts differently, and finally produce the finished product.

  • The data sources of a stream (like the elements selected by jQ) can be collections, arrays, and so on.
  • Popelinling returns the stream object itself with each operation. Multiple operations are connected in series to form a pipe.
  • A Stream provides a way to iterate internally, and a Stream can call the traversal method directly.

Use of the Stream

Access to flow

List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();

Set<String> set = new HashSet<>();
Stream<String> stream1 = set.stream();

Map<String,String> map = new HashMap<>();
Set<String> keySet = map.keySet();
Stream<String> stream2 = keySet.stream();

Collection<String> values = map.values();
Stream<String> stream3 = values.stream();

Set<Map.Entry<String, String>> entries = map.entrySet();
Stream<Map.Entry<String, String>> stream4 = entries.stream();

Stream<Integer> stream5 = Stream.of(5.5.5.5.5.5);
Copy the code

Commonly used method

  • Delay method
    • Method returns the object itself, supporting chained calls.
  • Put an end to the method
    • countandforEachAll the other methods are deferred.

If you are familiar with ES6’s new functions on arrays before Java, the following methods will look very familiar!

forEach
Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream.forEach(item->System.out.println(item)); // 1, 2, 3, 4, 5
Copy the code
filter
Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream
    .filter(item->item > 3)
    .forEach(item-> System.out.println(item)); / / 4 5
Copy the code
map
Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream
    .map(item->String.valueOf(item))
    .forEach(item-> System.out.printf(item)); / / "12345"

Copy the code
count
Stream<Integer> stream = Stream.of(1.2.3.4.5);
System.out.println(stream.count()); / / 5
Copy the code
limit
Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream.limit(3).forEach(item-> System.out.println(item)); / / 1 2 3
Copy the code
skip
Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream.skip(3).forEach(item-> System.out.println(item)); / / 4 5
Copy the code
concat

Merging two streams is a static method of Stream.

Stream<Integer> stream = Stream.of(1.2.3.4.5);
Stream<Integer> stream1 = Stream.of(6.7.8.9.10);
Stream<Integer> newStream = Stream.concat(stream, stream1);
newStream.forEach(item-> System.out.println(item));

Copy the code
Pay attention to the point

After each operation, data flows down the pipe to the next operation. The previous stream is also closed and cannot be called again.

Stream<Integer> stream = Stream.of(1.2.3.4.5);
stream
    .filter(item->item > 3)
    .forEach(item-> System.out.println(item)); / / 4 5
    
stream.forEach(item->System.out.println(item));// The stream is now closed, so an exception is thrown
Copy the code