java 8 stream group by example

package com.mkyong.java8;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Java8Examples4 {

    public static void main(String[] args) {

        //3 apple, 2 banana, others 1
        List<Item> items = Arrays.asList(
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 20, new BigDecimal("19.99")),
                new Item("orang", 10, new BigDecimal("29.99")),
                new Item("watermelon", 10, new BigDecimal("29.99")),
                new Item("papaya", 20, new BigDecimal("9.99")),
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 10, new BigDecimal("19.99")),
                new Item("apple", 20, new BigDecimal("9.99"))
                );

		//group by price
        Map<BigDecimal, List<Item>> groupByPriceMap =
			items.stream().collect(Collectors.groupingBy(Item::getPrice));

        System.out.println(groupByPriceMap);

		// group by price, uses 'mapping' to convert List<Item> to Set<String>
        Map<BigDecimal, Set<String>> result =
                items.stream().collect(
                        Collectors.groupingBy(Item::getPrice,
                                Collectors.mapping(Item::getName, Collectors.toSet())
                        )
                );

        System.out.println(result);

    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
group by using stream java stream group in java java stream in java 8 group by java stream api group by java streams group by java stream group by objects group java class object using stream java group in java stream java 8 stream api group by java 8 stream() grouping by group by in stream api java group by java stream group java stream stream group by java groupingby stream java 8 how to implement group by using streams java java stream grouping java java 8 stream group based on boolean java stream group by value group by id in streams java groupBy stream java 8 how to do group by Object[] in java 8 using streams streams java group by groupingBy java 8 stream list to a groupby java 8 groupby arraylist in java8 map groupby how to group a map java group values stream java java 8 collectors groupingby java 8 group by city java group data groupingby example in java 8 groupingBy java 8 java lambda groupingby mapping collect group by in java 8 java group by strem java stream grouping by java stream group by in group by java collectors groupingby java stream group by function groupby java 8 Java stream collect groupingby from list to map java 8 list stream group by example stream groupby example Group by in java streams collectors.groupby java stream map group by value grouping object java collectors.groupingby multiple fields java stream group by group java 8 java stream group by two lists java grouping by for each how to apply groupingby to list of integers in java 8 groupby in streams java collectors.groupingby java$ grouping by map grouping by examples group by java ! group by String java 8 grouping by java examples java 8 groupingby return list stream collectors grouping by java list to map groupby map to group by java collectors groupingby with joining grouping a list in java 8 stream.group java stream.groupby java stream group collectors groupingby mapping group map java java group by list java 8 grouping by if collectors groupingby grouping in streams java groupBy group list by property java groupingby in java 8 stream groupingby stream groupby java stream make group java grouping by java map group by java 8 group by id java collections group by groupingby in stream without list java stream groupby object groupby stream java group by stream java java stream groupingby map list java collections groupby collect to a map java 8 groupingby java list group by java group by stream group by in streams java java stream group by list to map of string and list java stream group by list to map java arraylist stream group by value java groupingby group a list of objects by an attribute java 8 groupby in java collectors.groupingby in java collectors groupingby examples stream api group by use using arraylist example group by using collection java 8 collector groupingby example group by java 8 groupingby java grouping by java 8 java 8 group list of map group by in java 8 string of list to group by in java 8 string to group by in java 8 groupby in java 8 collect a field from list of list of groups in java java group by java stream group list to map java stream groupby java stream groupingBy how to group data in stream java java 8 group stream java group by list to map group by java 8 stream api groupingby list&lt;object&gt; stream api group by list get object java 8 stream group by collectors groupingby java 8 example groupingby java 8 example groupingby in java 8 stream isgrouping java stream java 8 group by stream group by java 8 Collectors.groupingBy example collectors.groupingBy java stream group by java 8 in action lambdas streams and functional-style programming pdf java 8 group by list collectors groupingby example java 8 stream group by example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source