This is the sixth day of my participation in Gwen Challenge

Today, I’ll take a look at a very useful data processing method in Java 8 called Stream.

Everyone has used it in their development process. It’s incredibly useful. Basically, it’s fine to manipulate collections, arrays, filters, sorts, aggregates, and so on.

directory

What is Stream?

Second, generate flow

Third, the Stream method

Four, the flow of intermediate operations

5. Statistical cases

What is Stream?

The Java 8 API has added a new abstraction called a Stream that lets you process data in a declarative way.

Stream is an intuitive way to evaluate and express Java collections, similar to SQL statements querying data from a database.

 int sum = widgets.stream()
                      .filter(w -> w.getColor() == RED)
                      .mapToInt(w -> w.getWeight())
                      .sum();
Copy the code

A Stream is a Stream.

  • A Stream does not store elements, but evaluates them on demand.

  • A data source is the source of a stream. It can be a collection, an array, an I/O channel, a generator, etc.

  • Aggregation operations such as filter, Map, reduce, find, match, sorted, etc.

Second, generate flow

In Java 8, the collection interface has two methods to generate streams:

  • Stream () − Gets the data set of the collection object.
List<String> list = new ArrayList<>(); Stream<String> stream = list.stream(); ParallelStream <String> parallelStream = list.parallelstream (); // Get a parallel streamCopy the code
  • ParallelStream () − Creates parallel streams for collections.
Third, the Stream method
forEach

Through the data

Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 3).limit(5); Stream2. forEach(system.out ::println); // 0 3 6 9 12Copy the code
filter

Filter certain elements in the flow

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); long count = strings.stream().filter(string -> string.isEmpty()).count(); // The number of empty characters to filterCopy the code
limit

Get n elements

Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6); Stream2.foreach (system.out ::println); stream2.foreach (system.out ::println); // 0 2 4 6 8 10Copy the code
sorted

Natural ordering. Elements in a stream need to implement the Comparable interface

List<String> list = Arrays.asList("aa", "ff", "dd"); * list.stream().sorted().foreach (system.out ::println);Copy the code
Four, the flow of intermediate operations

Screening and sectioning

** Filter ** : Filters some elements in the stream ** DISTINCT: ** Removes duplicate elements by hashCode() and equals() of the elements in the streamCopy the code
Stream<Integer> stream = Stream.of( 7, 12, 9, 8, 10, 12, 14, 14); Stream<Integer> newStream = stream.filter(s -> s > 9) //12, 10, 12, 14, 14 newStream.forEach(System.out::println);Copy the code

mapping

Map: Transforms each element into a new element after being evaluated by the specified function

FlatMap: Each element is converted to a new stream by a specific function and joined together.

List<String> list = Arrays.asList("a,b,c"."1, 2, 3");
Stream<String> s1 = list.stream().map(s -> s.replaceAll(",".""));// Remove the comma from each element
s1.forEach(System.out::println); // abc 123
 
Stream<String> s3 = list.stream().flatMap(s -> {
    String[] split = s.split(",");
    Stream<String> s2 = Arrays.stream(split);
    return s2;
});
s3.forEach(System.out::println); // a b c 1 2 3
Copy the code
5. Statistical cases

In addition, Stream can be used as a means of statistical results. Int, double, long, and other basic types can be used:

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println(" maximum number in list: "+ stats.getmax ()); System.out.println(" minimum number in list: "+ stats.getmin ()); System.out.println(" sum: "+ stats.getsum ()); System.out.println(" average: "+ stats.getaverage ());Copy the code