Java Streams api sort functionality with comparator and reverse sorting using a productlist example

Published: 04 June 2024
on channel: CodeWis Technologies by Nuhman Paramban
7
0

Sorting on Increasing Number of Sales
java
Copy code
Comparator<Product> numberOfSalesIncreasingCompare = Comparator.comparing(Product::getNumberOfProductSold);
System.out.println(productList.stream().sorted(numberOfSalesIncreasingCompare).collect(Collectors.toList()));
A Comparator is created to sort products based on the number of products sold in increasing order.
The sorted method of the stream sorts the list using this comparator.
The sorted list is collected back into a list and printed.
Sorting on Decreasing Number of Sales
java
Copy code
Comparator<Product> numberOfSalesDecreasingCompare = Comparator.comparing(Product::getNumberOfProductSold).reversed();
System.out.println(productList.stream().sorted(numberOfSalesDecreasingCompare).collect(Collectors.toList()));
Another Comparator is created to sort products based on the number of products sold in decreasing order by using the reversed() method.
The sorted list is printed similarly to the previous example.
Alternative Way of Sorting by Number of Products Sold
java
Copy code
System.out.println("****Sorted by Number of product sold***");
List<Product> sortedByNumberOfProductsSold = productList.stream()
.sorted(Comparator.comparingInt(Product::getNumberOfProductSold))
.toList();
sortedByNumberOfProductsSold.forEach(System.out::println);
The list is sorted by the number of products sold using Comparator.comparingInt.
The sorted list is collected and printed using forEach.
Sorting by Number of Products Sold and Rating in Decreasing Order
java
Copy code
System.out.println("****Sorted by Number of product sold and Review Decreasing***");
List<Product> SortedByNumberOfProductSoldAndReviewDecreasing = productList.stream()
.sorted(Comparator.comparingInt(Product::getNumberOfProductSold)
.thenComparing(Product::getItemRating).reversed())
.toList();
SortedByNumberOfProductSoldAndReviewDecreasing.forEach(System.out::println);
This part sorts the list first by the number of products sold and then by the item rating, both in decreasing order.
The thenComparing method is used to add a secondary sorting criterion (item rating) after the primary sorting criterion (number of products sold).
The sorted list is printed using forEach.
Summary
Initialization: Creates a list of Product objects.
Sorting in Increasing Order: Sorts the list by the number of products sold in ascending order.
Sorting in Decreasing Order: Sorts the list by the number of products sold in descending order.
Alternative Sorting by Number of Products Sold: Another way to sort by the number of products sold in ascending order.
Sorting by Number of Products Sold and Rating: Sorts the list first by the number of products sold and then by rating, both in descending order.
This code demonstrates the use of Java Streams and Comparators to sort a list of products based on different criteria.


Watch video Java Streams api sort functionality with comparator and reverse sorting using a productlist example online, duration hours minute second in high quality that is uploaded to the channel CodeWis Technologies by Nuhman Paramban 04 June 2024. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 7 times and liked it 0 visitors.