Java Streams and Lambdas offer a powerful and expressive way to work with collections. In this article, we explore how to apply key stream operations like allMatch
, anyMatch
, and noneMatch
through a practical example involving a list of products. The example code demonstrates common predicate-based filtering using the Stream
API in a clean and readable manner.
Code Overview
Below is a Java class named ProductUtils
inside the com.nuhman.coding.practice
package. It uses a sample list of Product
objects and demonstrates how to evaluate certain conditions using stream operations:
java
package com.nuhman.coding.practice;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ProductUtils {
private static void learnStreamsAndLambdas() {
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)
);
// allMatch
System.out.println("****AllMatch***");
Predicate<Product> ratingGreaterThan4 = product -> product.getItemRating() > 4;
System.out.println(productList.stream().allMatch(ratingGreaterThan4));
// anyMatch
System.out.println("****AnyMatch***");
Predicate<Product> ratingGreaterThan4point5 = product -> product.getItemRating() > 4.5;
System.out.println(productList.stream().anyMatch(ratingGreaterThan4point5));
// noneMatch
System.out.println("****NoneMatch***");
Predicate<Product> ratingGreaterThan5 = product -> product.getItemRating() > 5;
System.out.println(productList.stream().noneMatch(ratingGreaterThan5));
}
public static void main(String[] args) {
learnStreamsAndLambdas();
}
}
Stream Operations Breakdown
🔹 Product List Initialization
A list of products is initialized using List.of(...)
. Each product includes:
- A name (e.g.,
"Apple iPhone 13"
) - A category (e.g.,
"Electronics"
) - A rating (e.g.,
4.8
) - A price (e.g.,
500
)
This list acts as the sample dataset for stream operations.
🔹 allMatch()
java
Predicate<Product> ratingGreaterThan4 = product -> product.getItemRating() > 4;
System.out.println(productList.stream().allMatch(ratingGreaterThan4));
- Purpose: Check if all products have a rating greater than 4.
- Result: Returns
true
only if every product satisfies the condition.
🟢 Use case: Validate data quality, e.g., ensure no low-rated products are listed.
🔹 anyMatch()
java
Predicate<Product> ratingGreaterThan4point5 = product -> product.getItemRating() > 4.5;
System.out.println(productList.stream().anyMatch(ratingGreaterThan4point5));
- Purpose: Check if at least one product has a rating greater than 4.5.
- Result: Returns
true
if any single product satisfies the condition.
🟢 Use case: Highlight standout products in a collection.
🔹 noneMatch()
java
Predicate<Product> ratingGreaterThan5 = product -> product.getItemRating() > 5;
System.out.println(productList.stream().noneMatch(ratingGreaterThan5));
- Purpose: Ensure that no product has a rating greater than 5.
- Result: Returns
true
if none of the products match the predicate.
🟢 Use case: Enforce upper limits or validation constraints.
Required Product Class
To run the above code successfully, make sure you have a corresponding Product
class defined, like this:
java
public class Product {
private String itemName;
private String category;
private double itemRating;
private double price;
public Product(String itemName, String category, double itemRating, double price) {
this.itemName = itemName;
this.category = category;
this.itemRating = itemRating;
this.price = price;
}
public double getItemRating() {
return itemRating;
}
// Additional getters/setters as needed
}
Conclusion
This example is a great introduction to functional programming in Java using Streams and Lambdas. With predicates and stream match operations (allMatch
, anyMatch
, and noneMatch
), you can quickly perform powerful evaluations over collections in a readable and concise way.
Whether you're working with real-world data or preparing for Java coding interviews, mastering these functional tools can significantly enhance your coding style and performance.