Preview:
import java.util.*;
import java.util.stream.Collectors;

class Product {
    int id;
    String name;
    double price;
    String type;
    double rating;

    Product(int id, String name, double price, String type, double rating) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.type = type;
        this.rating = rating;
    }

    public String toString() {
        return name + " (Price: " + price + ", Type: " + type + ", Rating: " + rating + ")";
    }
}

public class p11 {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product(1, "Laptop", 1500.0, "Electronics", 4.5),
                new Product(2, "Smartphone", 800.0, "Electronics", 4.8),
                new Product(3, "Refrigerator", 2000.0, "Home Appliances", 4.2),
                new Product(4, "TV", 300.0, "Electronics", 3.9),
                new Product(5, "Blender", 150.0, "Home Appliances", 4.1));

        System.out.println("Products with rating between 4 and 5:");
        products.stream().filter(p -> p.rating >= 4 && p.rating <= 5).forEach(System.out::println);

        System.out.println("\nFirst 2 products with price > 1000:");
        products.stream().filter(p -> p.price > 1000).limit(2).forEach(System.out::println);

        System.out.println("\nNumber of products under each type:");
        products.stream().collect(Collectors.groupingBy(p -> p.type, Collectors.counting()))
                .forEach((type, count) -> System.out.println(type + ": " + count));

        double averageRating = products.stream().filter(p -> p.type.equals("Electronics"))
                .mapToDouble(p -> p.rating).average().orElse(0.0);
        System.out.println("\nAverage rating of Electronics products: " + averageRating);
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter