https://www.mkyong.com/java8/java-8-collectors-groupingby-and-mapping-example/
Map<String, List<PieDataChart>> map = new HashMap<String, List<PieDataChart>>(); for (PieDataChart student : lstItemArry) { String key = String.valueOf(student.getProduct_id()); if(map.containsKey(key)){ List<PieDataChart> list = map.get(key); list.add(student); }else{ List<PieDataChart> list = new ArrayList<PieDataChart>(); list.add(student); map.put(key, list); } }
for (Map.Entry<String, List<PieDataChart>> entry : map.entrySet()) { // System.out.println("Key : " + entry.getKey() + " value : " + entry.getValue()); // for (int i=0 ; i< entry.getKey().length(); i++){ int sum = 0, value; for (int j = 0; j < entry.getValue().size(); j++){ value = Integer.valueOf(String.valueOf(entry.getValue().get(j))); //System.out.println(j+ "value = " + value); sum += value; // System.out.println(j+"chgevalue = " + sum); } // } System.out.println( "Key : " + entry.getKey() + "qty = " + sum); }
2.2 Group by the name + Count or Sum the
Qty.
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", 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"))
);
Map<String, Long> counting = items.stream().collect(
Collectors.groupingBy(Item::getName, Collectors.counting()));
System.out.println(counting);
Map<String, Integer> sum = items.stream().collect(
Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
System.out.println(sum);
}
}
Copy
output
//Group by + Count
{
papaya=1,
banana=2, apple=3, orang=1, watermelon=1
}
//Group by + Sum qty
{
papaya=20,
banana=30, apple=40, orang=10, watermelon=10
}
https://www.codingame.com/playgrounds/6162/6-ways-to-iterate-or-loop-a-map-in-java
Using entrySet()
// { autofold
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.Map;
public class
Main {
public static
void main(String[] args) {
// }
Map<Integer, String> customers = new
HashMap<>();
customers.put(1,
"Jhon");
customers.put(2,
"Smith");
customers.put(3,
"Sally");
for (Map.Entry<Integer, String> entry
: customers.entrySet()) {
System.out.println("Key : " +
entry.getKey() + " value : " + entry.getValue());
}
https://www.baeldung.com/java-array-sum-average
2.1. Sum Using a For Loop
In order to find the sum
of all elements in an array, we can simply iterate the array and add
each element to a sum accumulating variable.
This very simply starts
with a sum of 0 and add each item in the array as we go:
1
2
3
4
5
6
7
|
public static int findSumWithoutUsingStream(int[] array) {
int sum = 0;
for (int value : array) {
sum
+= value;
}
return sum;
}
|
Comments
Post a Comment