Stream Collectors Groupingby Example

Source: Internet
Author: User
Tags java 8 stream

In this article, we'll show you how to group, count, sum, and sort lists by using Java 8 Stream collectors. 1. Group by, Count and Sort

1.1 Group by A-list and display the total count of it. (Grouped by list and display totals) Java8example1.java

Package com.mkyong.java8;

Import Java.util.Arrays;
Import java.util.List;
Import Java.util.Map;
Import java.util.function.Function;
Import java.util.stream.Collectors;

public class Java8example1 {public

    static void Main (string[] args) {

        //3 Apple, 2 banana, others 1
        list<st ring> items =
                arrays.aslist ("Apple", "apple", "banana",
                        "apple", "orange", "banana", "papaya");

        map<string, long> result =
                Items.stream (). Collect (Collectors.groupingby (
                                ), collectors.counting ()
                        )
                ;

        SYSTEM.OUT.PRINTLN (result);


    }

Output

{
	papaya=1, orange=1, banana=2, Apple=3
}

1.2 Add sorting. (add sort implementation) Java8example2.java

Package com.mkyong.java8;
Import Java.util.Arrays;
Import Java.util.LinkedHashMap;
Import java.util.List;
Import Java.util.Map;
Import java.util.function.Function;

Import java.util.stream.Collectors;
        public class Java8example2 {public static void main (string[] args) {//3 Apple, 2 banana, others 1 list<string> items = arrays.aslist ("Apple", "apple", "banana", "apple", "Ora

        Nge "," banana "," papaya ");
                                map<string, long> result = Items.stream (). Collect (Collectors.groupingby (

        Function.identity (), collectors.counting ());

        map<string, long> finalmap = new linkedhashmap<> (); Sort a map and add to Finalmap Result.entryset (). Stream (). Sorted (map.entry.<string, long> Comparingbyvalue (). Reversed ()). foreachordered (E-> Finalmap.put (E.getkey (), E.getvalue ());


    System.out.println (FINALMAP); }
}

Output

{
	apple=3, banana=2, Papaya=1, Orange=1
}
2. List Objects

Examples to ' GROUP by ' a list of user defined Objects. (an example of grouping with the user-defined Objects list.) )

2.1 A Pojo. Item.java

Package com.mkyong.java8;

Import Java.math.BigDecimal;

public class Item {

    private String name;
    private int qty;
    Private BigDecimal Price;

    constructors, Getter/setters
}

2.2 Group BY the name + count or sum the Qty. (name + Count group or to Qty sum) Java8examples3.java

package Com.mkyong.java8;
Import Java.math.BigDecimal;
Import Java.util.Arrays;
Import java.util.List;
Import Java.util.Map;

Import java.util.stream.Collectors;
        public class Java8examples3 {public static void main (string[] args) {//3 Apple, 2 banana, others 1 list<item> items = arrays.aslist (new Item ("Apple", New BigDecimal ("9.99")), NE W Item ("Banana", new BigDecimal 

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.