iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Cheatsheet

A one-pager covering the modern Java syntax you reach for daily - streams, records, sealed types, virtual threads.

Common patterns

EXAMPLE
// Streams
List<Integer> evens = nums.stream()
    .filter(n -> n % 2 == 0)
    .toList();

// Optional
String name = Optional.ofNullable(user)
    .map(User::name)
    .orElse("anon");

// Records (Java 14+)
public record Point(int x, int y) {}

// Sealed types (Java 17+)
public sealed interface Shape permits Circle, Square {}

// Pattern matching for switch (Java 21)
String label = switch (shape) {
    case Circle c -> "circle r=" + c.radius();
    case Square s -> "square side=" + s.side();
};

// Text blocks
String json = """
    { "id": 1, "name": "a" }
    """;

// try-with-resources
try (var in = new FileInputStream(path)) {
    return in.readAllBytes();
}

// Concurrency - virtual threads
ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor();
Future<String> f = pool.submit(() -> fetch(url));

// Collectors
Map<String, Long> counts = items.stream()
    .collect(Collectors.groupingBy(Item::category, Collectors.counting()));

Why it matters

Keep this handy when you context-switch back from Kotlin or Go. Records, sealed types, and virtual threads moved Java forward fast - use them.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// javac | java | jar | jshell | mvn | gradle | jdeps
Try it Yourself »

Discussion

Loading…