Skip to content

Instantly share code, notes, and snippets.

@grommitz
Forked from raoulDoc/gist:fd997c5248285f903d65
Last active December 20, 2017 23:23
Show Gist options
  • Save grommitz/9f0fdd38c10a0bb61e88 to your computer and use it in GitHub Desktop.
Save grommitz/9f0fdd38c10a0bb61e88 to your computer and use it in GitHub Desktop.
fancy uses of collect in java 8 streams - done at a meetup with Raoul Gabriel Urma
@Test
public void mapEachTraderToTheSumOfTheirTransactions() {
//TODO: Create a Map<String, Integer> that maps each Trader's name with the sum of all its Transactions’ values
Map<String, Integer> mapNameToSumValue = new HashMap<>();
mapNameToSumValue =
transactions.stream()
.collect(groupingBy(transaction -> transaction.getTrader().getName(),
summingInt(transaction -> transaction.getValue())
));
assertEquals(1400, (int) mapNameToSumValue.get("Raoul"));
assertEquals(300, (int) mapNameToSumValue.get("Brian"));
assertEquals(950, (int) mapNameToSumValue.get("Alan"));
assertEquals(1410, (int) mapNameToSumValue.get("Mario"));
}
@Test
public void collectTheTransactionWithHighestValue() {
//TODO: What's the transaction with highest value? (using Collectors.maxBy)
Transaction highestTransaction = null;
Comparator<Transaction> byValue = comparing(Transaction::getValue);
highestTransaction =
transactions.stream()
.collect(collectingAndThen(maxBy(byValue), Optional::get));
assertEquals(transactions.get(1), highestTransaction);
}
@Test
public void mapEachYearToItsHighestTransaction() {
//TODO: Create a Map<Integer, Integer> that maps each year with the highest transaction value of that year
Map<Integer, Integer> yearToHighestValue = new HashMap<>();
Comparator<Transaction> byValue = comparing(Transaction::getValue);
// IntelliJ complaining but code is correct & running
yearToHighestValue =
transactions.stream()
.collect(groupingBy(transaction -> transaction.getYear(),
collectingAndThen(maxBy(byValue),
optionalTransaction -> optionalTransaction
.map(Transaction::getValue)
.get())
));
assertEquals(1000, (int) yearToHighestValue.get(2012));
assertEquals(400, (int) yearToHighestValue.get(2011));
}
@Test
public void mapEachTraderToTheirHighestTransaction() {
//TODO: Create a Map<String, Integer> that maps each Trader's name with their highest Transaction value
Map<String, Integer> mapNameToHighestValue = new HashMap<>();
Comparator<Transaction> byValue = comparing(Transaction::getValue);
mapNameToHighestValue =
transactions.stream()
.collect(groupingBy(transaction -> transaction.getTrader().getName(),
collectingAndThen(maxBy(byValue), optionalTransaction -> optionalTransaction
.map(Transaction::getValue)
.get())
));
assertEquals(1000, (int) mapNameToHighestValue.get("Raoul"));
assertEquals(300, (int) mapNameToHighestValue.get("Brian"));
assertEquals(950, (int) mapNameToHighestValue.get("Alan"));
assertEquals(710, (int) mapNameToHighestValue.get("Mario"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment