0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
Java · IntermediateSnippet

Functional Interfaces and Lambda Expressions

Functional interfaces contain exactly one abstract method and can be implemented using lambda expressions to provide a concise way to represent anonymous functions.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
@FunctionalInterface
interface MathOperation {
int operate(int a, int b);
}
public class LambdaExample {
public static void main(String[] args) {
MathOperation addition = (a, b) -> a + b;
System.out.println("Result: " + addition.operate(10, 5));
}
}
FunctionsSyntax
Java · IntermediateSnippet

Immutability with Records

Records are a special kind of class that acts as a transparent carrier for immutable data, automatically generating constructors, accessors, equals, and hashCode.

snippet.java
java
1
2
3
4
5
6
7
8
9
public record User(int id, String name) {}
public class RecordDemo {
public static void main(String[] args) {
User user = new User(1, "Alice");
System.out.println(user.name());
System.out.println(user);
}
}
OOPSyntax
Java · IntermediateSnippet

Stream API for Collection Processing

Streams allow functional-style operations on sequences of elements, such as filtering, mapping, and reducing collections efficiently.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
List<String> names = List.of("Anna", "Bob", "Charlie", "David");
List<String> filtered = names.stream()
.filter(n -> n.length() > 4)
.collect(Collectors.toList());
System.out.println(filtered);
}
}
FunctionsPerformance
Java · IntermediateSnippet

Safely Handling Null with Optional

The Optional class is a container object used to represent the presence or absence of a value, helping to avoid NullPointerExceptions.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Optional;
public class OptionalDemo {
public static void main(String[] args) {
Optional<String> name = Optional.ofNullable(getName());
String result = name.map(String::toUpperCase)
.orElse("DEFAULT");
System.out.println(result);
}
static String getName() { return null; }
}
Best PracticesError Handling
Java · IntermediateSnippet

Automatic Resource Management

The try-with-resources statement ensures that each resource is closed at the end of the statement, preventing memory leaks and resource exhaustion.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
import java.io.*;
public class TryWithResources {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Error HandlingBest Practices
Java · IntermediateSnippet

Exception Chaining for Better Debugging

Exception chaining allows you to wrap a low-level exception (like IOException) into a higher-level business exception while preserving the original cause. This keeps the stack trace intact for debugging.

snippet.java
java
1
2
3
4
5
6
7
public void loadConfig() throws DataException {
try {
Files.readAllLines(Paths.get("config.txt"));
} catch (IOException e) {
throw new DataException("Configuration file missing", e);
}
}
Error HandlingBest Practices
Java · IntermediateSnippet

Custom Annotations for Metadata

Custom annotations allow you to add metadata to your code that can be processed at runtime via Reflection. This is often used by frameworks for cross-cutting concerns like logging or validation.

snippet.java
java
1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionTime {
String value() default "Generic";
}
OOPBest Practices
Java · IntermediateSnippet

Memory Visibility with the volatile Keyword

The volatile keyword ensures that changes to a variable are always visible to all threads immediately. It prevents threads from caching the variable locally, ensuring they always read the latest value from main memory.

snippet.java
java
1
2
3
4
5
6
7
8
9
public class SharedFlag {
private volatile boolean active = true;
public void stop() { active = false; }
public void work() {
while (active) { /* perform task */ }
}
}
Async & ConcurrencyMemory Management