forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsExample.java
More file actions
112 lines (82 loc) · 2.96 KB
/
Copy pathCollectorsExample.java
File metadata and controls
112 lines (82 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package java8;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
class Item {
private String name;
private int qty;
private BigDecimal price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Item(String name, int qty, BigDecimal price) {
super();
this.name = name;
this.qty = qty;
this.price = price;
}
}
public class CollectorsExample {
public static void main(String[] args) {
//Group by a List and display the total count of it
List<String> items =
Arrays.asList("apple", "apple", "banana",
"apple", "orange", "orange", "orange", "orange", "banana", "papaya");
Map<String, Long> result =
items.stream()
.collect(
Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(result);
//Add sorting.
result.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue()
.reversed())
.forEach(x -> System.out.println(x));
//3 apple, 2 banana, others 1
List<Item> items1 = 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 the name + Count or Sum the Qty.
Map<String, Long> counting = items1.stream().collect(
Collectors.groupingBy(Item::getName, Collectors.counting()));
System.out.println(counting);
//Group by Price � Collectors.groupingBy and Collectors.mapping example.
Map<String, Integer> sum = items1.stream().collect(
Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
System.out.println(sum);
//Group by Price � Collectors.groupingBy and Collectors.mapping example.
Map<BigDecimal, Set<String>> result1 =
items1.stream().collect(
Collectors.groupingBy(Item::getPrice,
Collectors.mapping(Item::getName, Collectors.toSet())
)
);
System.out.println(result1);
}
}