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

Publicado el: 04 junio 2024
en el canal de: 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.


En esta página del sitio puede ver el video en línea Java Streams api sort functionality with comparator and reverse sorting using a productlist example de Duración hora minuto segunda en buena calidad , que subió el usuario CodeWis Technologies by Nuhman Paramban 04 junio 2024, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 7 veces y le gustó 0 a los espectadores. Disfruta viendo!