[Java] Stream Api--collect

Source: Internet
Author: User
Tags stream api

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.