This Java code defines a class ProductUtils within the package com.nuhman.coding.practice. It demonstrates the use of Java Streams and Lambdas with a Product class, performing several stream operations to evaluate certain conditions on a list of products.
Here's a breakdown of the code:
Package Declaration
java
Copy code
package com.nuhman.coding.practice;
The code is part of the com.nuhman.coding.practice package.
Imports
java
Copy code
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
The code imports several utility classes and interfaces from the java.util package, which are used for handling collections and stream operations.
Class Declaration
java
Copy code
public class ProductUtils {
The class ProductUtils is declared as public, meaning it can be accessed from other classes.
Main Method
java
Copy code
public static void main(String[] args) {
learnStreamsAndLambdas();
}
The main method is the entry point of the program. It calls the learnStreamsAndLambdas method to demonstrate the stream operations.
Method: learnStreamsAndLambdas
java
Copy code
private static void learnStreamsAndLambdas() {
This is a private static method within the ProductUtils class. It contains the logic for demonstrating stream operations.
Product List Initialization
java
Copy code
List<Product> productList = List.of(new Product("Apple iPhone 13", "Electronics", 4.8, 500),
new Product("Samsung Galaxy S21", "Electronics", 4.6, 450),
new Product("Nike Air Max 270", "Footwear", 4.7, 300),
new Product("Dyson V11 Vacuum Cleaner", "Home Appliances", 4.9, 150),
new Product("Instant Pot Duo 7-in-1", "Kitchen Appliances", 4.7, 200),
new Product("Sony WH-1000XM4 Headphones", "Electronics", 4.8, 250),
new Product("The Hobbit by J.R.R. Tolkien", "Books", 4.9, 100),
new Product("Patagonia Men's Better Sweater Fleece Jacket", "Clothing", 4.5, 50),
new Product("Lego Creator Expert Roller Coaster", "Toys & Games", 4.8, 75),
new Product("Olay Regenerist Micro-Sculpting Cream", "Beauty", 4.7, 120)
);
A list of Product objects is created using List.of(). Each Product has a name, category, rating, and price.
Stream Operations
AllMatch
java
Copy code
System.out.println("****AllMatch***");
Predicate<Product> ratingGreaterThan4 = product -> product.getItemRating() > 4;
System.out.println(productList.stream().allMatch(ratingGreaterThan4));
This block checks if all products have a rating greater than 4. The allMatch method returns true if all elements match the given predicate (ratingGreaterThan4).
AnyMatch
java
Copy code
System.out.println("****AnyMatch***");
Predicate<Product> ratingGreaterThan4point5 = product -> product.getItemRating() > 4.5;
System.out.println(productList.stream().anyMatch(ratingGreaterThan4point5));
This block checks if any product has a rating greater than 4.5. The anyMatch method returns true if any element matches the given predicate (ratingGreaterThan4point5).
NoneMatch
java
Copy code
System.out.println("****NoneMatch***");
Predicate<Product> ratingGreaterThan5 = product -> product.getItemRating() > 5;
System.out.println(productList.stream().noneMatch(ratingGreaterThan5));
This block checks if no products have a rating greater than 5. The noneMatch method returns true if no elements match the given predicate (ratingGreaterThan5).
Conclusion
The learnStreamsAndLambdas method demonstrates how to use Java Streams and Lambdas to perform common operations (allMatch, anyMatch, noneMatch) on a list of products. The main method invokes this method, and the results are printed to the console.
Note: For this code to work, there must be a Product class defined elsewhere in the package that includes the getItemRating() method and a constructor matching the instantiation in the productList.
More info https://github.com/nuhman85/coding
Информация по комментариям в разработке