Java Functional Programming With lambdas Limit Skip Min Max and Optionals

Опубликовано: 08 Июнь 2024
на канале: CodeWis Technologies by Nuhman Paramban
3
0

The code snippet demonstrates various operations on a collection of Product objects using Java Streams. Each operation is followed by printing the result to the console. Let's break down each section:

1. Skip the First Three Entries
java
Copy code
System.out.println("****Before Skip the first three entries from collection***");
List<Product> beforeSkipping = productList.stream().toList();
beforeSkipping.forEach(System.out::println);

System.out.println("**** After Skip the first three entries from collection***");
List<Product> skipFirstThree = productList.stream().skip(3).toList();
skipFirstThree.forEach(System.out::println);
Before Skipping:
Prints all products in productList.
After Skipping:
Creates a new list skipFirstThree by skipping the first three products from productList.
Prints the remaining products after skipping the first three.
2. Limit to the First Three Entries
java
Copy code
System.out.println("****Before Limit the first three entries from collection***");
List<Product> beforeLimitLastThree = productList.stream().toList();
beforeLimitLastThree.forEach(System.out::println);

System.out.println("**** After Limit the first three entries from collection***");
List<Product> AfterLimitLastThree = productList.stream().limit(3).toList();
AfterLimitLastThree.forEach(System.out::println);
Before Limiting:
Prints all products in productList.
After Limiting:
Creates a new list AfterLimitLastThree by limiting the stream to the first three products from productList.
Prints the first three products.
3. Find Maximum Value in a Collection with Comparator
java
Copy code
System.out.println("****Find a maximum value in a collection***");
Product findMaxNumberOfProductSold = productList.stream()
.max(Comparator.comparing(Product::getNumberOfProductSold))
.orElse(new Product());
System.out.println(findMaxNumberOfProductSold);
Find Max:
Finds the product with the maximum number of products sold using a comparator.
If no product is found (empty list), a new default Product is returned.
Prints the product with the maximum number of products sold.
4. Find Minimum Value in a Collection with Comparator
java
Copy code
System.out.println("****Find a minimum rated in a collection***");
Product findMinimumRatedProduct = productList.stream()
.min(Comparator.comparing(Product::getItemRating))
.orElse(new Product());
System.out.println(findMinimumRatedProduct);
Find Min:
Finds the product with the minimum item rating using a comparator.
If no product is found (empty list), a new default Product is returned.
Prints the product with the minimum item rating.
5. Find Minimum Rated Product with Rating Less Than 4
java
Copy code
System.out.println("****Find a minimum rated in a collection with rating less than 4***");
Optional<Product> findMinimumRatedProductWithRatingLessThan4 = productList.stream()
.min(Comparator.comparing(Product::getItemRating))
.filter(m -> m.getItemRating() < 4);
System.out.println(findMinimumRatedProductWithRatingLessThan4.isPresent());
Find Min with Condition:
Finds the product with the minimum item rating.
Filters the result to include only products with a rating less than 4.
Uses Optional to handle the case where no such product is found.
Prints whether a product with a rating less than 4 was found (true or false).
Summary
Skipping and Limiting: Demonstrates how to skip and limit entries in a stream.
Finding Min/Max: Uses comparators to find the minimum and maximum values in a collection.
Conditional Find: Combines finding a minimum value with an additional condition using Optional and filter.


Смотрите видео Java Functional Programming With lambdas Limit Skip Min Max and Optionals онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал CodeWis Technologies by Nuhman Paramban 08 Июнь 2024. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 3 раз и оно понравилось 0 посетителям.