1 .8 new features
lambda expression
data:
1 Consumer Interface.
HTTPS: // blog.csdn.net/qq_28410283/article/details/80618456
2 .java8 the Lambda Expressions Introduction
HTTPS: // blog.csdn.net/qq_28410283/ Article This article was / Details / 80,961,022
3. novice tutorial. 8 the Java the Lambda expressions
HTTPS: // www.runoob.com/java/java8-lambda-expressions.html
consumer semantic meaning of consumption,
public interface consumer <T> {
void Accept ( t T);
// consumer
//Reference is mainly made into a series of operations, in the stream, the mainly used forEach; internal iteration when the incoming parameters to do a number of operational, no return value;
default Consumer <T> andthen ( Consumer <? Super T> the After)
// pass a Consumer types of parameters, its generic type,
// with this interface is the same T, do first interface to accept this operation,
// and then do the incoming Consumer accept the type of operating parameters
simple example Lambda expressions:
1. no arguments, returns a value of 5
() -> 5
2 receives a parameter (type number), which returns a value twice.
X -> 2 * X
. 3 . accepts two parameters (number), and return to their difference
(X, Y) -> X - Y
. 4 int . 2 receives int type integer, and return to their
( X, int Y) -> X + Y
. 5 . Accepts a string object, and printed on the console, nothing is returned (return looks like void)
(String S) -> of System.out.print (S)
Lambda expressions used to note the following points:
Lambda expression method is mainly used to perform the type of interface definition line, for example, a simple way interface. In the above example, we used Lambda expressions to define the various types of methods MathOperation interface. Then we define the execution sayMessage.
Lambda expressions eliminates the trouble of using anonymous methods, and given Java a simple but powerful function of the programming capabilities.
Variable Scope
lambda expression can only refer to mark the final outer local variables, which means that the definition of local variables can not be modified within the extraterritorial application of lambda, otherwise compilation errors.
local variable lambda expressions can not be declared as final , but must not be modified later in the code (i.e., having a recessive final semantic)
Stream Flow Operation
Information:
1.Java of Stream Streaming
HTTPS: // blog.csdn.net/qq_20989105/article/details/81234175
2.Java 8 Stream | newbie tutorial // actual tutorial
https://www.runoob.com/java/java8 -streams.html
Introduction:
the Java 8 Stream is enhanced collection (collection) function of the object, it focuses on a collection of objects of all kinds is very convenient and efficient aggregation operations (aggregate operation),
or mass data operations (bulk data operation). Lambda expressions Stream API by means of the same emerging, which greatly improves the efficiency of programming and program readability. At the same time it provides serial
and parallel modes to be aggregated operation, concurrency model can take advantage of multi-core processors, using the fork / the Join in parallel to accelerate the process and split task. Usually I write parallel code is
difficult and error-prone, but using Stream API without writing a single line of multi-threaded code, you can easily write high-performance concurrent programs. So, the Java 8 in the first occurrence of
java.util.stream is a functional language + product of the combined effects of multi-core era.
Sequence: | Stream Elements of + -----> | + filter -> | the sorted + -> | Map + -> | the collect |
generate the flow
set interface has two methods to generate a stream:
Stream () - Create a serial stream as a set .
parallelStream () - Create a set of parallel streams.
Example:
List <String> = Arrays.asList strings ( "ABC", "", "BC", "EFG", "ABCD", "", "JKL" );
List <String> Filtered = strings.stream () (! String -> .filter string.isEmpty ()). the collect (Collectors.toList ());
forEach
the Random Random = new new the Random ();
random.ints () limit (. 10 ) .forEach (the System.out :: println);
<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
method for filtering out the conditions set by the element. The following code fragment uses filtration filter out null string:
limit
the method for obtaining a specified number of streams. The following code fragment uses limit method to print out 10 pieces of data:
the sorted
process for sorting the stream. The following code fragment uses the method of sorted output 10 random numbers are sorted:
the Random Random = new new the Random ();
random.ints () limit (. 10 ) .sorted () forEach (the System.out :: the println);.
ParallelStream
Parallel program stream in place of the parallel processing program. We use the following examples parallelStream outputs an empty string number
List <String> = Arrays.asList strings ( "ABC", "", "BC", "EFG", "ABCD", "", "JKL" );
// Get the number of empty string
int COUNT = strings. . parallelStream () filter (String -> string.isEmpty ()) COUNT ();.
Collectors
Collectors class implements a number of reduction operations, for example, flow into the collection and aggregation of 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 ()) the collect (Collectors.toList ());
the System.out.
MergedString String . Strings.stream = () filter (String -> string.isEmpty ()!) The collect (Collectors.joining ( ",." ));
System.out.println ( "Merge string:" + mergedString);
statistics
addition, some produce statistical results of the collection is also very useful. They are mainly used int, Double basic types, long and the like, which may be used to generate statistics similar to the following.
List <Integer> = Arrays.asList Numbers (. 3, 2, 2,. 3,. 7,. 3,. 5 );
IntSummaryStatistics stats = numbers.stream () mapToInt ((X) ->. X) .summaryStatistics ();
the System. Out.println ( "maximum number of list:" + stats.getMax ());
System.out.println ( "minimum number list:" + stats.getMin ());
System.out.println ( "sum of all the numbers:" + stats.getSum ());
System.out.println ( "Mean:" + stats.getAverage ());