{"dataset":{"id":"00000000-0000-0000-0000-000000000063","slug":"java-best-practices","name":"Java best practices","description":"Source-linked guidance on Java fundamentals: classes and interfaces, collections, exceptions, concurrency, and streams. Scoped to current Java docs.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Processing data with the Stream API","sourceUrl":"https://dev.java/learn/api/collections-and-streams/streams/map-filter-reduce/","text":"The Stream API, added in Java SE 8, implements the map-filter-reduce algorithm as a companion to the Collections Framework, which stores and organizes data in JVM memory. Open a stream on a collection, or create streams on I/O or custom data sources. Mapping transforms objects one-to-one via a Function, filtering selects elements via a Predicate, and reducing aggregates like SQL's COUNT, SUM, MIN, MAX, or AVERAGE. Intermediate operations return streams and process nothing until a terminal operation consumes elements one at a time; a stream stores no data. Call only one method per stream instance — reuse throws IllegalStateException — so create and consume streams on the spot, never modify the source, and avoid side effects. Scope: Java. Reviewed October 4, 2026.","id":"java-best-practices:Processing data with the Stream API","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Virtual threads for concurrent throughput","sourceUrl":"https://dev.java/learn/api/virtual-threads/","text":"Platform threads incur nontrivial costs, requiring thousands of CPU instructions to start and consuming megabytes of memory, making separate platform threads infeasible for high-concurrency server applications. Instead of non-blocking APIs with deeply nested callbacks, use virtual threads to make blocking cheap. Since Java 21, virtual threads allow familiar structures like branches and loops during I/O waits, as the underlying platform thread unmounts the blocking virtual thread to run others. Utilize Executors.newVirtualThreadPerTaskExecutor() to create an ExecutorService handling large numbers of concurrent, I/O-blocking tasks. Avoid virtual threads for CPU-intensive work; use parallel streams or recursive fork-join tasks instead. Note that while JEP 425 introduced them in Java 19, they transitioned through preview in Java 20 before finalization. Scope: Java. Reviewed October 4, 2026.","id":"java-best-practices:Virtual threads for concurrent throughput","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Catching and handling exceptions","sourceUrl":"https://dev.java/learn/language/annotations-exceptions/exceptions/catching-handling/","text":"Enclose exception-throwing code in a try block followed directly by one or more catch blocks; no code can appear between the try block's end and the first catch. Short methods can use a single try block with multiple handlers. Order catch blocks from specific to general: IOException is a super type of FileNotFoundException, so catching IOException first leaves the FileNotFoundException handler unreachable and the compiler reports an error. The compiler flags checked exceptions like the FileWriter constructor's IOException but not unchecked ones like IndexOutOfBoundsException from list.get(i). Put cleanup code in a finally block, which always executes when the try block exits and prevents resource leaks, or use the try-with-resources statement from Java SE 7 to close AutoCloseable resources automatically. Scope: Java. Reviewed October 4, 2026.","id":"java-best-practices:Catching and handling exceptions","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"The Collections Framework organization","sourceUrl":"https://dev.java/learn/api/collections-and-streams/collections-framework/organization/","text":"Implement the Iterable interface to enable iteration over objects, as it serves as the super interface for all Collection hierarchy members and was introduced in Java SE 5 alongside the for-each pattern. While not part of the Collections Framework itself, Iterable allows instances to be used with this loop structure by providing an Iterator. For storing elements, leverage the Collection interface, which models common behaviors shared by List and Set. Utilize Collection operations to add or remove elements, test for element presence, determine size or emptiness, and clear content. Perform set operations such as union, intersection, and complement. Access elements via iterators or by creating parallel streams. These operations are also available on List and Set. Scope: Java. Reviewed October 4, 2026.","id":"java-best-practices:The Collections Framework organization","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Creating classes: declarations, fields, and constructors","sourceUrl":"https://dev.java/learn/language/oop/classes-objects/creating-classes/","text":"A class declaration lists, in order: modifiers such as public or private, the class name capitalized by convention, an optional superclass after extends (a class can only extend one parent), and implemented interfaces after implements (more than one allowed), before the brace-surrounded class body. The class body provides the life cycle of objects: constructors initialize new objects, field declarations provide state, and methods implement behavior. Declare fields with zero or more modifiers, a type, and a name; fields are member variables in a class, distinct from local variables in a method or block and parameters in method declarations. In the spirit of encapsulation, it is common to make fields private, exposing values indirectly through public methods. Scope: Java. Reviewed October 4, 2026.","id":"java-best-practices:Creating classes: declarations, fields, and constructors","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}