I. R collect (Supplier Supplier, Biconsumer Accumulator, Biconsumer combiner)
Supplier: A way to create an instance of a target type.
Accumulator: A method that adds an element to the target.
Combiner: A way to combine multiple results from an intermediate state (which is used in concurrency)
New Arraylist<> (), (list, item), List.add (item), (one, both), One.addall (a);
II, R collect (Collector Collector)
The collector is actually the supplier, accumulator, combiner of the above aggregation body. Then the above code becomes:
= Stream.of (1, 2, 3, 4). Filter (P, p > 2). Collect (Collectors.tolist ());
Third, collector
Collector is a variable reduction operator interface for stream, and the Collectors (class collector) provides many common implementations of variable reduction operations.
Iv. creation of collector
Convert to other collections: ToList, Toset, Tocollection, Tomap
list<integer> collectlist = Stream.of (1, 2, 3, 4) . Collect (Collectors.tolist ()); System.out.println ("collectlist:" + collectlist); // Print Results // collectlist: [1, 2, 3, 4]
turn into a value:
Use collect to convert a stream to a value. Maxby and Minby allow users to generate a value in a particular order.
-
-
- averagingdouble: Leveling Mean, Stream has an element type of double
- averagingint: Average, the element type of stream is int
- Averaginglong: Average, Stream has an element type of long
- counting:stream Number of elements
- Maxby: The largest element of a stream under a specified condition
- minby: The smallest element of a stream under a specified condition
- reducing:reduce Action
- summarizingdouble: Counts the data (double) state of the stream, including Count,min,max,sum and averages.
- Summarizingint: Counts the data (int) state of the stream, including Count,min,max,sum and averages.
- Summarizinglong: Counts the data (long) state of the stream, which includes count,min,max,sum and averages.
- summingdouble: Sum, Stream has an element type of double
- summingint: Sum, Stream has an element type of int
- Summinglong: Request And, the element type of stream is long
optional<integer> Collectmaxby = Stream.of (1, 2, 3, 4) (o))) ; System.out.println ("Collectmaxby:" + Collectmaxby.get ()); // Print Results // Collectmaxby:4
Split data block: Collectors.partitioningby
Map<boolean, list<integer>> Collectparti = Stream.of (1, 2, 3, 4) (it% 2 = 0)); System.out.println ("Collectparti:" + Collectparti); // Print Results // Collectparti: {false=[1, 3], true=[2, 4]}
Data group: Collectors.groupingby
Map<boolean, list<integer>> collectgroup= stream.of (1, 2, 3, 4) (It > 3)); System.out.println ("Collectgroup:" + collectgroup); // Print Results // Collectgroup: {false=[1, 2, 3], true=[4]}
String: collectors.joining
String Strjoin = Stream.of ("1", "2", "3", "4") . Collect (Collectors.joining (",", "[", "]")); System.out.println ("strjoin:" + strjoin); // Print Results // strjoin: [1,2,3,4]
[Java] Stream Api--collect