Comparator thenComparingLong() method in Java with examples

The thenComparingLong(java.util.function.ToLongFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a Long sort key.

Syntax:

default Comparator <T> thenComparingLong(
    ToLongFunction <T> keyExtractor)

Parameters: This method accepts keyExtractor which is the function used to extract the Long sort key.



Return value: This method returns a lexicographic-order comparator composed of this and then the Long sort key.

Exception: This method throws NullPointerException if the argument is null.

Below programs illustrate thenComparingLong(java.util.function.ToLongFunction) method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate Comparator
// thenComparingLong(ToLongFunction) method
  
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
  
public class GFG {
    public static void main(String... args)
    {
        List<Users> list = createUsers();
        System.out.printf("before sort: %s%n", list);
        Collections.sort(list,
                         Comparator
                             .comparing(Users::getDepartment)
                             .thenComparingLong(Users::getId));
        System.out.printf("after sort: %s%n", list);
    }
  
    private static List<Users> createUsers()
    {
        return Arrays.asList(
            new Users(12311, "ME"),
            new Users(10211, "CSE"),
            new Users(33111, "CSE"),
            new Users(21000, "IT"),
            new Users(12133, "IT"),
            new Users(21445, "CSE"));
    }
  
    private static class Users {
        private long id;
        private String department;
  
        public Users(long id, String department)
        {
            this.id = id;
            this.department = department;
        }
  
        public long getId()
        {
            return id;
        }
  
        public void setId(long id)
        {
            this.id = id;
        }
  
        public String getDepartment()
        {
            return department;
        }
  
        public void setDepartment(String department)
        {
            this.department = department;
        }
  
        @Override
        public String toString()
        {
            return department + " - " + id;
        }
    }
}

chevron_right


The output printed on console of IDE is shown below.
Output:

You can see in example first sorting is done on department wise and if the department is same then ID wise.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate Comparator
// thenComparingLong(ToLongFunction) method
  
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
  
public class GFG {
    public static void main(String... args)
    {
  
        List<String> list
            = Arrays.asList("KKR", "CSK",
                            "MI", "KXIP", "RCB",
                            "SRH", "DC", "RR");
  
        try {
  
            // apply thenComparingLong
            Comparator.comparing(list::get)
                .thenComparingLong(null);
        }
        catch (Exception e) {
  
            System.out.printf("Exception:" + e);
        }
    }
}

chevron_right


The output printed on console is shown below.
Output:

References: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#thenComparingLong(java.util.function.ToLongFunction)()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.