On java8 stream of new features

Stream data structure is not, there is no internal storage structures; only fetch data from other data structure, it will not modify the data underlying their encapsulated data structure. Meanwhile, stream similar to an iterator (the Iterator), unidirectional, not reciprocating, data can only be traversed once traversed once after exhausted.

 

1, stream creation:

1.1, static factory method: Stream <String> stringStream = Stream.of ( "aa", "bb", "cc");

1.2, and default method Collection interface array: int arr [] = new int [] {1, 2, 3}; Arrays.stream (arr) .forEach (System.out :: println);

 

2, stream of common operations:

2.1、map

// convert uppercase, wordList as a set of words List <String> type 
List <String> output = wordList.stream ( ) map (String :: toUpperCase) .collect (Collectors.toList ()).;
// This code generates the square of a list of integers {1, 4, 9, 16}. 
List <Integer> = Arrays.asList the nums (. 1, 2,. 3,. 4); 
List <Integer> = squareNums nums.stream () Map (n--> n-n-*) .collect (Collectors.toList ()).;

2.2、filter

// left even, through the "divisible by 2" of the filter condition of the rest of the number is {2, 4, 6}. 
Integer [] = {sixNums. 1, 2,. 3,. 4,. 5,. 6}; 
Integer [] = Stream.of Evens (sixNums) .filter (n--> n-% 2 == 0) .toArray (Integer []: : new new); 
// each line word by flatMap finishing to the new Stream, and then retain the length is not zero, that is, all words in the whole article. 
// REGEXP as a regular expression, particularly the specific logic analysis 
List <String> reader.lines Output = (). 
    FlatMap. (Line -> Stream.of (line.split (REGEXP,))) 
    filter (Word -> word.length ()> 0) .collect (Collectors.toList ());

2.3、foreach

// print all the names of men, roster as a person collection of type List <Pserson> 
// the Java 8 
roster.stream () filter (the p--> p.getGender () == Person.Sex.MALE). .ForEach (the p-- > System.out.println (p.getName ())); 
// the Java-PreS. 8 
for (the Person P: the roster) { 
    IF (p.getGender () == Person.Sex.MALE) { 
        System.out.println (p.getName ()); 
    } 
}

2.4、findFirst

It always returns the first element of Stream, or empty.

2.5、Optional

This is a concept to imitate Scala language, as a container, it may contain a certain value, or does not contain. Its purpose is to use as much as possible to avoid NullPointerException.

2.6、reduce

The main role of this method is to combine elements of Stream. It provides a start value (seed), then in accordance with the calculation rule (BinaryOperator), and a front of the Stream, the second, the n-th element combination. In this sense, string concatenation, sum value, min, max, average are special reduce. There is no case where the starting value, then the first two elements will combine Stream, returns Optional.

2.7、limit/skip

Return to the previous limit of n elements Stream; Skip is discarded before the n elements

2.8、min/max/distinct

min and max functions can also be sorted by first Stream element, then findFirst achieved, but the former will be better performance, is O (n), the cost is sorted in O (n log n). At the same time they are independent as a special reduce method because it is seeking the maximum and minimum is a very common operation.
2.9, Match

There are three methods Stream match, semantically:

allMatch: Stream in line with all the elements of the incoming predicate, returns true

anyMatch: Stream as long as there is an element in line with the incoming predicate, returns true

noneMatch: Stream none of the elements in line with the incoming predicate, returns true

 

Guess you like

Origin www.cnblogs.com/stupid-chan/p/11227544.html