Java basic types of optimization 8

In Java, the generic reference type must be used, and can not use basic types. Therefore, when the list to represent the basic type or array, you must be substantially corresponds to the type of reference type, such as
intcorrespondence Integerlongcorrespondence Long, etc., may be automatically boxing and unboxing basic types and reference types, but there will be some loss of performance.

It provides a Java Stream API 8's ofmethod, if the primary type of parameters passed, in fact, get inside is a type of Stream reference. The code below

1
2
// get the type Integer Stream 
Stream <Integer> = Stream.of Stream (. 1, 2,. 3);

Therefore, Java provides some basic types optimized specifically for the API 8, such as  IntStreamLongStreamDoubleStream, priority should be given to use them.

definition

Define the basic types of Stream, may be used IntStreamLongStreamDoubleStreamthe interface to the static method ofrangeempty, the following is an example of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create an empty IntStream 
IntStream = IntStream.empty empty (); 

// Create basic types comprising 1, 2, 3 IntStream 
IntStream intStream IntStream.of = (1, 2, 3); 

// Create a 1 to IntStream 9 of 
IntStream IntStream.range Range = (. 1, 10); 

// Create a IntStream 1 through 10 
IntStream rangeClosed IntStream.rangeClosed = (. 1, 10); 

// Create a IntStream 3 of 
IntStream generated = IntStream. Generate (() ->. 3); 

// get an infinite loop IntStream, value. 1,. 3,. 5, .... 7 
IntStream IntStream.iterate infinite = (. 1, the operand -> the operand + 2);

You can also create IntStream based int array, such as

1
2
3
4
5
int [] Array = new new int [] {. 1, 2,. 3, 4,. 5}; 
// entire array comprising IntStream 
IntStream arrayStream = Arrays.stream (Array); 
// index into the array location comprising 2 start, end 4 (not included) is IntStream 
IntStream rangeArray = Arrays.stream (Array, 2,. 4);

use

Basic types of Stream, internal interface functions are related to the basic type of operation, thus avoiding the corresponding boxed reference types,  IntStream mainly in the following method of operation

filter method

filterThe method of data Stream in the filter, which receives an  IntPredicate interface, which is an  int -> boolean interface function, the following example code

1
2
// 输出 2, 4, 6
IntStream.of(1, 2, 3, 4, 5, 6, 7).filter(elem -> elem % 2 == 0).forEach(System.out::println);

map method

mapThe method is mainly the elements of some kind of Stream mapping, a value converted into another basic type, which receives an  IntUnaryOperator interface, which is a  int -> int function interface, the following sample code:

1
2
// 输出 10, 20, 30, 40, 50, 60, 70
IntStream.of(1, 2, 3, 4, 5, 6, 7).map(elem -> elem * 10).forEach(System.out::println);

mapToObj method

mapToObj The method is mainly the elements in the Stream boxing operation, it is converted into a reference value type, which receives an  IntFunction interface, which is a  int -> R function interface, the following sample code:

1
2
// 输出a1, a2, a3, a4, a5, a6, a7
IntStream.of(1, 2, 3, 4, 5, 6, 7).mapToObj(elem -> "a" + elem).forEach(System.out::println);

mapToLong method

mapToLong Stream approach is to convert the basic types of elements in longwhich a receiving  IntToLongFunction interface, which is a  int -> long function interface, the following example code

1
2
// 输出 100, 200, 300, 400, 500, 600, 700
IntStream.of(1, 2, 3, 4, 5, 6, 7).mapToLong(elem -> elem * 100L).forEach(System.out::println);

mapToDouble method

mapToDouble Stream approach is to convert the basic types of elements in doublewhich a receiving  IntToDoubleFunctioninterface, which is a  int -> double function interface, the following example code

1
2
// 输出 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1
IntStream.of(1, 2, 3, 4, 5, 6, 7).mapToDouble(elem -> elem + 0.1).forEach(System.out::println);

min Method

min The method of obtaining the minimum element Stream, it returns  OptionalInt the type, which is a version of the int Optional, also in order to avoid packing unpacking operations. The following sample code

1
2
// output. 1 
System.out.println (IntStream.of (. 1, 2,. 3,. 4) .min () getAsInt ().);

summaryStatistics method

summaryStatistics The main method is to obtain statistical information elements Stream, it returns  IntSummaryStatistics, the following sample code:

1
2
3
4
5
6
7
8
9
10
11
Summary IntStream.of = IntSummaryStatistics (. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10) .summaryStatistics (); 
// output. 1 
System.out.println (summary.getMin ()); 
/ / output 10 
System.out.println (summary.getMax ()); 
// output 55 
System.out.println (summary.getSum ()); 
// output 10 
System.out.println (summary.getCount ()); 
// output 5.5 
System.out.println (summary.getAverage ());

In addition,  flatMapdistinctsortedpeeklimitskipforEachreducecollectanyMatchallMatchnoneMatchand other methods, and Streammethods are similar.

Guess you like

Origin blog.csdn.net/ryuenkyo/article/details/89010920