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.
Смотрите видео Java Streams api sort functionality with comparator and reverse sorting using a productlist example онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал CodeWis Technologies by Nuhman Paramban 04 Июнь 2024. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 7 раз и оно понравилось 0 посетителям.