public record Transaction(
long id,
String category,
double amount,
String description,
LocalDate date
) {
public Transaction {
if (id <= 0) throw new IllegalArgumentException(
"ID must be positive");
Objects.requireNonNull(category);
if (amount <= 0) throw new IllegalArgumentException(
"Amount must be positive");
}
}
// Filter and collect
List<Transaction> filtered =
transactions.stream()
.filter(t -> t.amount() >= 50.0)
.toList();
// Group by category
Map<String, Double> totals =
transactions.stream()
.collect(Collectors.groupingBy(
Transaction::category,
Collectors.summingDouble(
Transaction::amount
)
));
// Filter directly
val filtered = transactions
.filter(_.amount >= 50.0)
// Group and sum in one pass
val totals = transactions
.groupMapReduce(_.category)(_.amount)(_ + _)
// Filter directly
val filtered = transactions
.filter { it.amount >= 50.0 }
// Group and calculate totals
val totals = transactions
.groupBy { it.category }
.mapValues { (_, txns) ->
txns.sumOf { it.amount }
}
String json = """
{
"name": "%s",
"email": "%s",
"active": %b
}
""";
System.out.println(json.formatted("Alice", "alice@example.com", true));
Use List.of() to create immutable lists in Java 9+.
Similarly, Set.of() and Map.of() provide immutable collections.
In Scala, collections are immutable by default: List("a", "b", "c")
Kotlin uses listOf() for read-only lists and mutableListOf() for mutable ones.
| Element | Color | Dracula Name |
|---|---|---|
Keywords (public, class, if) |
â– Pink | #ff79c6 |
| Strings | â– Yellow | #f1fa8c |
| Functions | â– Green | #50fa7b |
| Classes/Types | â– Cyan | #8be9fd |
| Numbers | â– Purple | #bd93f9 |
| Comments | â– Blue-Gray | #6272a4 |