Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Comparator.comparingLong method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

comparingLong is a static method of the Comparator interface that is used to return a Comparator object. comparingLong uses a sort key of type long to sort the elements in a list. The key is extracted with the help of the implementation of the ToLongFunction functional interface. You can learn more about the ToLongFunction here.

The Comparator interface is defined in the java.util package. To import the Comparator interface, use the following import statement.

import java.util.Comparator;

Syntax

public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)

Parameters

  • ToLongFunction<? super T> keyExtractor: Function to extract the sort key.

Return value

The method returns a Comparator.

Code

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Main{
static class Person{
long salary;
public Person(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"salary=" + salary +
'}';
}
}
public static void main(String[] args) {
Person person1 = new Person(1000);
Person person3 = new Person(432343);
Person person2 = new Person(34532345);
List<Person> personList = Arrays.asList(person1, person2, person3);
System.out.println("Before sorting: " + personList);
personList.sort(Comparator.comparingLong(value -> value.salary));
System.out.println("After sorting: " + personList);
}
}
Implementation of the comparingLong method

Explanation

  • In lines 1-3, we import the relevant packages.
  • In line 7, we define a Person class with salary as the attribute.
  • In lines 23-25, we create different Person objects with different salary values.
  • In line 26, we create a list of created Person objects.
  • In line 27, we print the list of created Person objects before sorting.
  • In line 28, we use the comparingLong method to sort the list, and we create an implementation of the ToLongFunction interface that extracts the salary attribute from the Person objects.
  • In line 29, we print the list of Person objects after sorting.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring