-
-
Save raoulDoc/fd997c5248285f903d65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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