java 8 stream group by example
Java
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);
}
}
Also in Java:
- Title
- nextchar in java
- Category
- Java
- Title
- how to remove all items from alist in java
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- 2 decimal places print format JAVA
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- priorityqueue poll
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- in java how to convert string to integer
- Category
- Java
- Title
- jar file not running after being compiled
- Category
- Java
- Title
- java create arraly list
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- butterfly pattern program in java
- Category
- Java
- Title
- how to test for legit email in java
- Category
- Java
- Title
- java hashmap get value
- Category
- Java
- Title
- how to add strings in java
- Category
- Java
- Title
- java stack methods
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- java printf tab
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- do while loop java
- Category
- Java
- Title
- check if a list contains a string java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- method resolve file in java
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- java mysql springboot jpa stackoverflow
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- java parse xml string
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- java create file
- Category
- Java
- Title
- generate objects with for loop java
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- Linked List implementation
- Category
- Java
- Title
- vector length java
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java
- Title
- java define main function
- Category
- Java
- Title
- how to exit a java program
- Category
- Java
- Title
- java script num toSting syntax eror
- Category
- Java
- Title
- java get excectuon time
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- java array to arraylist
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- reverse shuffle merge
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- spigot title
- Category
- Java
- Title
- how to import jframe in java
- Category
- Java
- Title
- two dimensional array in java example program
- Category
- Java
- Title
- how to compare string in java
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- intellij replace all
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- find maven version
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- length of string java
- Category
- Java
- Title
- java 8 seconds to days
- Category
- Java
- Title
- java array swap
- Category
- Java
- Title
- java iterator example
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- java switch display panel
- Category
- Java