Javaexercise.com

Sorting Stream In Reverse Order Using Java Lambda

To sort a Java stream in reverse order using lambda expressions, we can use the sorted() method of the stream in combination with the Comparator interface, which allows us to define a custom ordering for the elements of the stream.

To reverse the ordering, we can use the reverseOrder() method of the Comparator interface. Here's an example:

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/* 
 *  Code example to sort stream in reverse order using java lambda
 */
	
public class JExercise {

	public static void main(String[] args) throws IOException {

		// Creating list
		List<Integer> numbers = Arrays.asList(5, 2, 4, 1, 3);
		
		// Converting list to stream and sorting the elements in reverse order
		List<Integer> sortedNumbers = numbers.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
		
		// Printing elements to the console
		System.out.println(sortedNumbers);
	}
}

Output:

[5, 4, 3, 2, 1]
 

In this example, the sorted() method is called with the Comparator.reverseOrder() method, which returns a comparator that reverses the natural ordering of the elements.

The resulting stream is then collected into a list using the collect() method.

Reverse Stream with custom ordering

You can also use a lambda expression to define a custom ordering for the elements, and then reverse that ordering by calling the reversed() method on the resulting Comparator object.

Here's an example:

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/* 
 *  Code example to sort stream in reverse order using java lambda
 */

public class JExercise {

	public static void main(String[] args) throws IOException {

		// Creating list
		List<String> list = Arrays.asList("ashish kumar", "basit somaniama", "chil", "david");

		// Converting list to stream and sorting the elements in reverse order
		List<String> sortedList = list.stream()
				.sorted((s1, s2) -> s1.length() - s2.length()) // sort by length
				.sorted(Comparator.comparingInt(String::length).reversed()) // reverse the order
				.collect(Collectors.toList());

		// Printing elements to the console
		System.out.println(sortedList);
	}
}

Output:

[basit somaniama, ashish kumar, david, chil]
 

In this example, the sorted() method is called twice: first with a lambda expression that sorts the elements by their length, and then with a Comparator.comparingInt() method that creates a comparator based on a function that returns the length of each element.

The resulting comparator is then reversed using the reversed() method. The resulting stream is then collected into a list using the collect() method.

Sorting stream using the sorted() method

To sort a stream in reverse order using a Java lambda, you can use the sorted() method along with the Comparator interface.

You can create a Comparator object that compares two elements in the reverse order, and then pass it to the sorted() method.

Here's an example:

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/* 
 *  Code example to sort stream in reverse order using java lambda
 */

public class JExercise {

	public static void main(String[] args) throws IOException {

		// Creating list
		List<Integer> list = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);

		// Converting list to stream and sorting the elements in reverse order
		List<Integer> sortedList = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

		// Printing elements to the console
		System.out.println(sortedList);
	}
}

Output:

[9, 6, 5, 5, 4, 3, 3, 2, 1, 1]

 

In this example, we first created a list of integers called numbers. We then created a stream from this list using the stream() method.

We sorted this stream in reverse order using the sorted() method and passing Comparator.reverseOrder() as an argument, which creates a comparator that sorts in the reverse order.

Finally, we collected the sorted elements back into a list using the collect() method and printed it.

Sorting stream in reverse order with lambda expression

Another way to sort a stream in reverse order using a Java lambda is to use the sorted() method along with a lambda expression that defines the comparison between two elements.

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/* 
 *  Code example to sort stream in reverse order using java lambda
 */

public class JExercise {

	public static void main(String[] args) throws IOException {

		// Creating list
		List<Integer> list = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);

		// Converting list to stream and sorting the elements in reverse order
		List<Integer> sortedList = list.stream().sorted((a, b) -> b.compareTo(a)).collect(Collectors.toList());

		// Printing elements to the console
		System.out.println(sortedList);
	}
}

Output:

[9, 6, 5, 5, 4, 3, 3, 2, 1, 1]
 

In this example, we passed a lambda expression to the sorted() method that defines the comparison between two elements.

The lambda expression (a, b) -> b.compareTo(a) compares two elements in reverse order by calling the compareTo() method on the second element b and passing the first element as an argument.

Sorting stream using reverseOrder() method

Another approach is to use the Comparator.reverseOrder() method to obtain a comparator that sorts elements in reverse order, and then pass it to the sorted() method of the stream.

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/* 
 *  Code example to sort stream in reverse order using java lambda
 */

public class JExercise {

	public static void main(String[] args) throws IOException {

		// Creating list
		List<Integer> list = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);

		// Converting list to stream and sorting the elements in reverse order
		List<Integer> sortedList = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

		// Printing elements to the console
		System.out.println(sortedList);
	}
}

Output:

[9, 6, 5, 5, 4, 3, 3, 2, 1, 1]

 

In this example, we used the Comparator.reverseOrder() method to obtain a comparator that sorts elements in reverse order.

We then passed this comparator to the sorted() method of the stream to obtain a new stream that contains the elements of the original stream sorted in reverse order.

Conclusion

To sort a stream in reverse order using Java lambda expressions, you can use the sorted() method of the stream with a lambda expression that defines a custom ordering for your elements. You can also use the Comparator.reverseOrder() method to obtain a comparator that sorts elements in reverse order, and then pass it to the sorted() method of the stream. Both approaches are valid and have their own advantages and disadvantages.