Close Menu
Şevket Ayaksız
    What's Hot

    Java Period and Duration: Define What “One Day Later” Means

    November 11, 2026

    Oracle LEFT JOIN Filters: Preserve the Rows Your Report Needs

    November 11, 2026

    Java Comparator Null Handling: Decide Where Missing Values Belong

    November 11, 2026
    • software
    • Gadgets
    Şevket AyaksızŞevket Ayaksız
    • Home
    • Technology

      Apple: iPhone 17 and Older Models Hit With Unexpected $100 Price Increase

      September 10, 2026

      Apple Watch Intelligence Could Be a Major Accessibility Breakthrough

      September 10, 2026

      Apple May Skip the Base iPhone 18 This Year

      September 10, 2026

      FCC changes robot vacuum rules, potentially affecting future models

      August 8, 2026

      Samsung’s new 2TB 990 SSD drops to its lowest price yet

      August 6, 2026
    • Adobe

      Photoshop Smart Filters: Keep Effects Adjustable Throughout an Edit

      November 11, 2026

      Illustrator Scaling: Keep Strokes and Effects in Proportion

      November 10, 2026

      Illustrator Global Swatches: Make Color Changes Follow Your Design

      November 8, 2026

      Adobe Acrobat Bookmarks: Give Long PDFs a Useful Navigation Structure

      November 7, 2026

      How to Keep a Photoshop Adjustment on the Right Layer

      November 6, 2026
    • Microsoft

      Excel Structured References: Write Formulas That Explain Their Data

      November 11, 2026

      Word Pagination: Keep a Heading with the Paragraph That Follows

      November 10, 2026

      PowerPoint Selection Pane: Find and Manage Objects Behind Other Objects

      November 10, 2026

      Excel Data Validation: Guide Entry and Check What Arrives Later

      November 8, 2026

      PowerPoint Speaker Notes: Prepare the Message Behind the Slides

      November 7, 2026
    • java

      Java Period and Duration: Define What “One Day Later” Means

      November 11, 2026

      Java Comparator Null Handling: Decide Where Missing Values Belong

      November 11, 2026

      Java String.split: Preserve Empty Fields When Their Position Matters

      November 10, 2026

      Java String Case Conversion: Choose the Locale Explicitly

      November 8, 2026

      Java Optional: Choose Between a Default Value and Deferred Work

      November 7, 2026
    • Oracle

      Oracle LEFT JOIN Filters: Preserve the Rows Your Report Needs

      November 11, 2026

      Oracle Running Totals: Choose ROWS or RANGE Before Trusting the Result

      November 10, 2026

      Oracle NULLIF: Handle a Zero Denominator Without Inventing a Result

      November 8, 2026

      Oracle WHERE and HAVING: Filter the Right Level of Your Report

      November 8, 2026

      Oracle ROW_NUMBER: Make “Latest Record” Queries Deterministic

      November 7, 2026
    Şevket Ayaksız
    Anasayfa » Java Comparator Null Handling: Decide Where Missing Values Belong
    java

    Java Comparator Null Handling: Decide Where Missing Values Belong

    By sevketayaksizNovember 11, 2026No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Java Comparator Null Handling: Decide Where Missing Values Belong
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Sorting becomes a product decision when some values are missing. Should an incomplete record appear first so someone can correct it, or last so completed records remain easy to browse? The answer should be deliberate.

    Java’s comparator utilities make that decision expressible in code. The important part is applying the null policy at the correct level and checking how it interacts with ascending or descending order.

    Define the desired result first

    Suppose an application displays appointment names, with a few entries still unnamed. A simple requirement might be: sort the available names alphabetically and place missing names at the end.

    For a list of nullable strings, that policy can be represented as:

    Comparator<String> nameOrder =
        Comparator.nullsLast(Comparator.naturalOrder());

    The Comparator documentation states that nullsLast considers null greater than non-null values and delegates comparisons between non-null values to the supplied comparator. Two null values compare as equal.

    The code now records a visible policy. A reviewer does not need to infer the intended placement from scattered conditional statements.

    Reverse the part you mean to reverse

    Descending order introduces a subtle choice. Consider these expressions:

    Comparator<String> first =
        Comparator.nullsLast(Comparator.<String>naturalOrder())
                  .reversed();
    
    Comparator<String> second =
        Comparator.nullsLast(Comparator.reverseOrder());

    The first reverses the entire comparison, including the placement of nulls. The second reverses the ordering of non-null values while retaining the outer nulls-last policy.

    For an illustrative list containing “Cedar”, “Maple”, and null, the second expression puts “Maple” before “Cedar” and leaves the missing value last.

    Write down the expected sequence before choosing the expression. That small step prevents a user-interface requirement from being lost in a fluent chain of method calls.

    Separate missing records from missing fields

    A null object and an object containing a null field are different cases.

    For records with an optional name, a comparator can apply the policy to the extracted field:

    Comparator<Customer> order =
        Comparator.comparing(
            Customer::name,
            Comparator.nullsLast(Comparator.naturalOrder())
        );

    This assumes that the Customer objects themselves are non-null. If the collection may also contain null customers, the outer comparator needs a separate policy.

    Keeping those assumptions explicit helps the team decide where invalid data should be rejected and where incomplete data is acceptable.

    Test the order as a complete sequence

    A useful test contains ordinary values, at least one null, and duplicate values. Assert the full expected order rather than checking only that sorting did not throw an exception.

    If equal names need a predictable secondary order, add a meaningful tie-breaker such as a unique identifier. Choose it according to the display requirement.

    Also remember that sorting a list with the standard collection sorting utilities requires a list that supports the necessary modifications. Java documents stable sorting for Collections.sort, preserving the relative order of elements that compare as equal.

    Finally, review how the interface labels missing values. Placing them correctly is only half the task; readers should also understand whether the blank means unknown, not supplied, or not applicable.

    Sources

    • Oracle — Comparator API
    • Oracle — Collections API
    Views: 2
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    sevketayaksiz
    • Website

    Related Posts

    Java Period and Duration: Define What “One Day Later” Means

    November 11, 2026

    Java String.split: Preserve Empty Fields When Their Position Matters

    November 10, 2026

    Java String Case Conversion: Choose the Locale Explicitly

    November 8, 2026
    Add A Comment

    Comments are closed.

    Şevket Ayaksız
    Instagram
    • Home
    • Adobe
    • microsoft
    • java
    • Oracle
    • Contact
    © 2026 Theme Designed by Şevket Ayaksız.

    Type above and press Enter to search. Press Esc to cancel.