java8 .stream().map().collect()

Steam () : a source of the data, may be a set, arrays, I / O channel, generator and other generator, converted into a stream.

mylist.stream()
	.map(myfunction->{
		return item;
	}).collect(Collectors.toList());

forEach () : iteration for each data stream. The following code fragment uses forEach 10 outputs random numbers.

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

Map ()
: for mapping each element corresponding to a result. The following code fragment uses the output of the map corresponding to the square of the number of elements:
List <Integer> = Arrays.asList Numbers (. 3, 2, 2,. 3,. 7,. 3,. 5);
// obtain a corresponding number of squares
List <Integer> squaresList numbers.stream = () Map (I -> I * I) .distinct () the collect (Collectors.toList ());..
filter () : a method for filtering a filter condition set by the element. The following code fragment uses filtration filter out null string:
List <String> = Arrays.asList strings ( "ABC", "", "BC", "EFG", "ABCD", "", "JKL");
/ / Get the number of empty string
int = strings.stream COUNT () filter (string -> string.isEmpty ()) COUNT ();..
limit
limit the method for obtaining a specified number of streams. The following code fragment uses limit data 10 to print out the method:

the Random Random new new = the Random ();
. Random.ints () limit (10) .forEach (the System.out :: the println);

the sorted () : Convection for sorting. The following code fragment uses the method of sorted random numbers output 10 sort:
The Random Random new new = the Random ();
. Random.ints () limit (10) .sorted () forEach (the System.out :: the println);.
Parallel (Parallel) program
parallelStream stream is replaced by a parallel method of processing program. The following examples we used the number of output parallelStream empty string:

List <String> = Arrays.asList strings ( "ABC", "", "BC", "EFG", "ABCD", "", "JKL");
// Get the number of empty string
int = strings.parallelStream COUNT () filter (string -> string.isEmpty ()) COUNT ();..

Collectors () : class implements a number of reduction operations, for example, flow into collection and polymeric elements. Collectors may be used to return a list or string:
List <String> = Arrays.asList strings ( "ABC", "", "BC", "EFG", "ABCD", "", "JKL");
List <String> filtered = strings.stream () filter.. (string -!> string.isEmpty ()) collect (Collectors.
 

. String mergedString = strings.stream () filter (String -> string.isEmpty ()!) The collect (Collectors.joining ( ","));.
System.out.println ( "Merge string:" + mergedString);


https://www.cnblogs.com/HowieYuan/p/9394552.html


description link: https: //blog.csdn.net/shine_guo_star/article/details/94383319

Guess you like

Origin www.cnblogs.com/agnesFlower/p/12125028.html