Challenge: Factory Product Filtering
Завдання
Swipe to start coding
The factory produces products represented by Product
objects, which have the fields id
, weight
, and quality
.
- Filter the products to keep only those with a
quality
of"GOOD"
and aweight
greater than 10.0. - Extract only the
id
of the filtered products using a method reference. - Once the result is collected into a list, print the
id
of the selected products using a method reference toprintln
.
Рішення
solution.java
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат