Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Factory Product Filtering | Intermediate Operations in Stream API
Stream API

book
Challenge: Factory Product Filtering

Завдання

Swipe to start coding

The factory produces products represented by Product objects, which have the fields id, weight, and quality.

  1. Filter the products to keep only those with a quality of "GOOD" and a weight greater than 10.0.
  2. Extract only the id of the filtered products using a method reference.
  3. Once the result is collected into a list, print the id of the selected products using a method reference to println.

Рішення

solution.java

solution.java

package com.example;

import java.util.List;

public class Main {
public static void main(String[] args) {
List<Product> products = List.of(
new Product(1, 10.0, "GOOD"),
new Product(2, 8.2, "DEFECTIVE"),
new Product(3, 12.3, "GOOD"),
new Product(4, 10.5, "GOOD"),
new Product(5, 11.0, "GOOD")
);

List<Integer> passingProductIds = products.stream()
.filter(p -> p.getQuality().equals("GOOD") && p.getWeight() > 10.0)
.map(Product::getId)
.toList();

passingProductIds.forEach(System.out::println);
}
}

class Product {
private int id;
private double weight;
private String quality;

public Product(int id, double weight, String quality) {
this.id = id;
this.weight = weight;
this.quality = quality;
}

public int getId() {
return id;
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
single

single

package com.example;

import java.util.List;

public class Main {
public static void main(String[] args) {
List<Product> products = List.of(
new Product(1, 10.0, "GOOD"),
new Product(2, 8.2, "DEFECTIVE"),
new Product(3, 12.3, "GOOD"),
new Product(4, 10.5, "GOOD"),
new Product(5, 11.0, "GOOD")
);

List<Integer> passingProductIds = products.stream()
.___(p -> p.getQuality().equals(___) && ___ > 10.0)
.___(___)
.toList();

passingProductIds.___(___);
}
}

class Product {
private int id;
private double weight;
private String quality;

public Product(int id, double weight, String quality) {
this.id = id;
this.weight = weight;
this.quality = quality;
}

public int getId() {
return id;

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

We use cookies to make your experience better!
some-alt