package com.ant.jdk8.chap06;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GroupingTransaction {
public static void main(String[] args) {
List<Transaction> transactions = Arrays.asList(
new Transaction(Currency.EUR, 1500.0),
new Transaction(Currency.USD, 2300.0),
new Transaction(Currency.GBP, 9900.0),
new Transaction(Currency.EUR, 1100.0),
new Transaction(Currency.JPY, 7800.0),
new Transaction(Currency.CHF, 6700.0),
new Transaction(Currency.EUR, 5600.0),
new Transaction(Currency.USD, 4500.0),
new Transaction(Currency.CHF, 3400.0),
new Transaction(Currency.GBP, 3200.0),
new Transaction(Currency.USD, 4600.0),
new Transaction(Currency.JPY, 5700.0),
new Transaction(Currency.EUR, 6800.0) );
transactions.stream()
.collect(Collectors.groupingBy(Transaction::getCurrency))
.forEach((c,list)-> System.out.println(c+":"+list));
}
}
收集器Collectors主要提供了三大功能:
1.将流元素归约和汇总为一个值
2.元素分组
3.元素分区