import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Main {
public static void main(String[] args) {
/**
* Scenario: I have a list of Transaction objects (id, amount, currency).
* I want you to sum the total amount for a specific currency (e.g., "USD"),
* but only for transactions over $1000.
*/
List<Transaction> transactions = List.of(new Transaction(1, 1000.0, "USD"),
new Transaction(2, 1900.0, "USD"),
new Transaction(3, 3000.0, "EUR"),
new Transaction(4, 2000.0, "EUR"),
new Transaction(5, 100.0, "USD"),
new Transaction(6, 1100.0, "EUR"),
new Transaction(7, 5000.0, "MXN"));
// We have a list of transactions. We need to sum the anount of the transactions by currency
// but only taking into account the ones greater to $1000
// We can collect them and use the groupingBy collector. Then we can use
// the filtering collector to filter transactions and only include
// with amount greater than 1000
// Funally, we can pass the summingDouble downstream collector to summarize the
// amounts
Map<String, Double> transactionsByCurrency = transactions.stream()
.collect(Collectors.groupingBy(t -> t.currency(), Collectors.filtering(t -> t.amount() > 1000.0, Collectors.summingDouble(t -> t.amount()))));
System.out.println(transactionsByCurrency);
}
}
record Transaction(int id, double amount, String currency) {}