Javatpoint Logo
Javatpoint Logo

Java List Sort Lambda

Java is a well-known computer language that has been in use for more than two decades. Several essential features enable developers to construct efficient and elegant code. For example, the List sort() function is used to sort a List in ascending or descending order. In this tutorial, we will look at how to utilize the Java List sort() function with lambda expressions.

A Java List is an ordered collection that allows us to save and modify a collection of items. We need to sort a List depending on a given criterion, such as alphabetical order, numerical order, or custom order, from time to time.

Lambda expressions were introduced as a new language feature in Java 8. In Java, a lambda expression is a lightweight and succinct means of representing a function. It is made up of a collection of arguments, an arrow sign (->), and a function body. Lambda expressions may be used to construct functional interfaces containing a single abstract method.

Before we get into the specifics, let's first define a lambda expression. A lambda expression is a shorthand means of representing an anonymous function that may be provided as a parameter. It's a slang term for a technique that may be used to construct functional interfaces.

Let's now look at the List sort() function. Sorting the members of a List in ascending or descending order is accomplished using the sort() function. It comes in two varieties:

The sort() method's first variation is used to sort List entries in ascending order, whereas the second variant is used to sort List elements in descending order.

To utilize the List sort() function with lambda expressions, we must first construct a Comparator object, which compares two List members and delivers a result depending on their comparison. The Comparator object may then be sent as an argument to the sort() function.

Let's look at some real-world instances to understand how this works.

Example 1: Sorting a String List in ascending order

Output:

[apple, banana, kiwi, orange]

In this example, we use lambda expressions to generate and sort a List of Strings in ascending order. We supply a lambda expression that compares two Strings and returns a value based on the results. The sort() function then sorts the List using this lambda expression.

Example 2: Sorting an Integer List in Descending Order

Output:

[8, 6, 5, 2, 1]

In this example, we generate a List of Integers and use lambda expressions to sort it in descending order. A lambda expression that compares two integers is passed and returns a value dependent on the comparison's outcome.

Example 3: Ascending the order of a list of custom objects

ListSort.java

Output:

[Alice (22), Bob (30), John (25), Mary (27)]

Explanation

We define a Person class in this example with two fields: name and age. For your convenience, we additionally supply a constructor, getters, and an override toString() function.

We generate a List of Person objects in the main() function and feed it to the sort() method along with a lambda expression that compares two Person objects based on their names. This lambda expression is used by the sort() function to sort the List in ascending order of names.

Finally, we use System.out to output the sorted List. println().







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA