This is the 25th day of my participation in the August Challenge.

The Stream (Java8)

New Java 8 features use streams, which travel through pipes and can be processed on the nodes of pipes, such as filtering, sorting, aggregation, and so on. The flow of elements in the pipe is processed by the intermediate operation, and the final operation yields the result of the previous operation

A Stream is a queue of elements from a data source and supports aggregation operations: Where the data source is the source of the stream, the source can be set, array, etc., and the set operations can be filter, map, reduce, find, match, sorted, etc. We can view the stream operations through the code:

  • filter

    In fact, it is a filter operation, set the filter conditions for filtering

/ / 1. String a List < String > strings = Arrays. The asList (" a ", ""," bb ", "CCC", "DDDD", ""," efg "); //2. Count = strings.stream().filter(string-> string.isempty ()).count(); System.out.println(" Empty string number: "+ count);Copy the code

The data source is strings.stream(); The number of empty strings is: 2. We can look at several filter operations:

count = strings.stream().filter(string -> string.length() == 3).count(); System.out.println(" the number of strings of length 3 is: "+ count); filtered = strings.stream().filter(string ->! string.isEmpty()).collect(Collectors.toList()); System.out.println(" Filtered list: "+ filtered); mergedString = strings.stream().filter(string ->! string.isEmpty()).collect(Collectors.joining(", ")); System.out.println(" merge string: "+ mergedString);Copy the code

  • map

The map method is used to map each element to the corresponding result

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
squaresList1 = numbers.stream().map( i ->i*i).collect(Collectors.toList());
squaresList2 = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
System.out.println("Squares1 List: " + squaresList1);
System.out.println("Squares2 List: " + squaresList2);
Copy the code

SquaresList1 is directly calculating the value of each array square And corresponding to the corresponding position, while squaresList2 is one to one correspondence But go to But no change relative to the location of the output of results:

Squares1 List: [9, 4, 4, 9, 49, 9, 25]

Squares2 List: [9, 4, 49, 25]

  • mapToInt

Returns an IntStream consisting of the result of the element to which the given function is applied.

A List < Integer > integers = Arrays. AsList (1,2,13,4,15,6,17,8,19); IntSummaryStatistics stats = integers.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

Return IntSummaryStatistics describing the various summary data about this flow element.

In addition to having a mapToInt, there’s a mapToDouble, similar to mapToLong

  • limit

The limit method is used to get a specified number of streams

// limit output to 2 Random numbers Random = new Random(); random.ints().limit(5).sorted().forEach(System.out::println);Copy the code

Where IntStream ints() returns a Stream of validly infinite pseudo-random int values (as if it were the result of calling method nextInt()). The sorted method is used to sort the Stream. ForEach: Stream provides the new method ‘forEach’ to iterate over each data in the Stream

conclusion

Using Stream can greatly improve the efficiency of our development process and the code looks clean and concise. Major share today is that I’m going to use in developing some of the stream flow and more content can refer to the API documentation: docs.oracle.com/javase/8/do…