Article

    Modern Java unleashed: virtual threads revolution & other game-changing features in JDK 21

    Today, we celebrate the 30th anniversary of one of the most enduring and influential programming languages – Java. Since its debut in 1995, Java has shaped the software development landscape, powering everything from web and mobile apps to enterprise systems and big data solutions. Its versatility, reliability and platform independence have made it a cornerstone of modern development.

    To celebrate the occasion, we invited Ivan Mihov, Senior Java Engineer at News UK Tech team. With extensive experience in consulting and designing scalable systems, Ivan shares his insights into Java’s evolution and some of the key features in JDK 21.

    From virtual threads to cleaner code constructs

    Java 21 is more than a version bump - it's a leap toward modern concurrency, better collections, cleaner conditional and data access logic. While past releases added convenience, Java 21 introduces tools that can reshape how we write scalable, expressive and maintainable Java.

    Virtual threads

    What are Virtual threads?

    Example with Explicit Virtual threads

    Spring Boot integration (zero code change)

    REST client example

    Virtual threads vs CompletableFuture

    Spring data JPA in Virtual threads

    Other Virtual threads limitations

    Sequenced collections

    01 The problem they solve
    02 Example usage
    03 Common pitfalls
    04 Real use case: LRU cache
    05 Audit trail buffer example

    Prior to Java 21, Set and Map didn’t provide guaranteed first/last element access in a uniform way. SequencedCollection, SequencedSet and SequencedMap fix this by standardising ordered access.

    Integration with Existing Classes: These new interfaces fit neatly into the existing collections hierarchy - List and Deque now extend SequencedCollection, LinkedHashSet implements SequencedSet and SortedSet has SequencedSet as its immediate superinterface. This means existing collection classes you already use gain these powerful methods automatically.

    Sequenced _ollections_example_usage

    This API brings clarity to cases where ordering matters but isn’t explicitly encoded in the type.

    • Using a regular HashSet assumes no order. Now you can enforce expectations with sequenced interfaces.
    • Some legacy code assumes ordering based on insertion – now it's formalised

    Real_Use_Case_LRU_Cache

    You now have predictable, spec-defined ordering for eviction strategies.

    Sequenced collections are perfect for maintaining audit trails:

    Audit_Trai_Buffer_Example

    It improves both readability and intent when working with time-ordered operations.

    Pattern matching for switch

    Why it matters

    Switches in Java used to be tied to primitives or enums. Now you can switch on object types, and the compiler does both the instanceof check and cast.

    Example

    Pattern_Matching_for_Switch_example

    Clean, concise, no explicit instanceof or casts.

    Pitfalls

    • Must be exhaustive, or the compiler will force a default.
    • Doesn’t replace polymorphism when business logic varies per type.

    Real use case: JSON dispatching

    Real_Use_Case_JSON_Dispatching

    Improves readability in parsers and API gateways.

    Domain rule evaluation

    Pattern matching is excellent in DSLs or business rule engines:

    Domain_Rule_Evaluation

    The power of "when" guards: Pattern matching in switch becomes even more powerful with "when" clauses. These guards let you add arbitrary boolean conditions to pattern matches, combining type checking and value testing in a single step.

    In this example, after matching Integer types and extracting the value into the "age" variable, the when clause filters matches based on their values, creating a clean alternative to nested if-else chains. This approach adds clarity to complex business rules and removes deeply nested conditional logic.

    Record patterns

    Icon Black Goal@3x

    What they do

    Record patterns let you deconstruct records inline, reducing boilerplate and making your code look declarative.

    Seamless Backward Compatibility: Record patterns work seamlessly with all existing record classes - no modifications needed to your records. This means you can immediately start using pattern matching with records you've already defined in your codebase, unlocking a more declarative style without any migration effort.

    Icon Black Goal@3x

    Basic example

    Record_Patterns_example

    Compare that to using p.x() and p.y() after type checks and casting.

    Icon Black Goal@3x

    Match in switch

    Match_in_Switch

    You can pair record patterns inside pattern matching for switch to write even more declarative code.

    Icon Black Goal@3x

    Real use case: event processing

    Real_Use_Case_Even_Processing

    This is perfect for modelling finite domain workflows like state machines or event buses.

    Icon Black Goal@3x

    Nested record unpacking

    Record patterns allow matching deep structures in one step:

    Nested_Record_Unpacking

    No need to extract and cast each component.

    Summary table

    Here's how these features transform Java development:

    Feature Why It Matters Real-World Value
    Virtual Threads Enables concurrency without complexity Run blocking code without killing scalability
    Sequenced Collections Gives order to hashed data Clear APIs for LRU, FIFO queues, audit trails
    Pattern Switch Simplifies logic based on type Faster, clearer decision trees in apps
    Record Patterns Declarative data unpacking Easier DTO/event parsing, no boilerplate

    Final words

    Java 21 arms you with the tools to write modern, clean and scalable code without relying on external libraries or complex patterns. Virtual Threads make high-concurrency apps a reality for every Java team, even if developers don’t want to write async-style code, while pattern matching and record patterns strip away old verbosity. And sequenced collections? They finally bring order and an enhanced API to hashed data optimised for quick access.


    Ready to rethink your Java?

    uk_london 1
    uk_london 1
    uk_london 1
    uk_london 1
    uk_london 1

    Join Nortal

    Check out our vacancies here.

    Get in touch

    Nortal is a strategic innovation and technology company with an unparalleled track-record of delivering successful transformation projects over 20 years.