-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.java
More file actions
65 lines (49 loc) · 1.9 KB
/
Copy pathProduct.java
File metadata and controls
65 lines (49 loc) · 1.9 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package Streams;
import java.util.*;
public class Product implements Comparable<Product> {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public int compareTo(Product otherProduct) {
return Double.compare(this.price, otherProduct.price);
}
public static void main(String[] args) {
List<Product> productList = new ArrayList<>();
productList.add(new Product(1, "Laptop", 1200.00));
productList.add(new Product(2, "Smartphone", 800.00));
productList.add(new Product(3, "Headphones", 150.00));
productList.add(new Product(4, "Tablet", 500.00));
productList.add(new Product(5, "Camera", 700.00));
productList.add(new Product(6, "Printer", 300.00));
productList.add(new Product(7, "Mouse", 25.00));
productList.add(new Product(8, "Keyboard", 50.00));
productList.add(new Product(9, "Monitor", 250.00));
productList.add(new Product(10, "External Hard Drive", 120.00));
OptionalDouble sum = productList.stream()
.filter((p) -> p.getPrice() > 500)
.mapToDouble((p) -> p.getPrice())
.average();
productList.stream()
.sorted((p1,p2) -> ((Double)p1.getPrice()).compareTo(p2.getPrice()))
.forEach((p) -> System.out.println(p.getPrice()));
productList.stream()
.sorted()
.forEach((p) -> System.out.println(p.getPrice()));
System.out.println(sum);
}
}