-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsExample.java
More file actions
65 lines (51 loc) · 1.65 KB
/
CollectorsExample.java
File metadata and controls
65 lines (51 loc) · 1.65 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 com.interview;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
class ProductCollect{
private int id;
private String name;
private float price;
public ProductCollect(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
public class CollectorsExample {
public static void main(String[] args) {
List<ProductCollect> productsList = new ArrayList<ProductCollect>();
//Adding Products
productsList.add(new ProductCollect(1,"HP Laptop",25000f));
productsList.add(new ProductCollect(2,"Dell Laptop",30000f));
productsList.add(new ProductCollect(3,"Lenevo Laptop",28000f));
productsList.add(new ProductCollect(4,"Sony Laptop",28000f));
productsList.add(new ProductCollect(5,"Apple Laptop",90000f));
Set<Float> set = productsList.stream()
.map(product->product.getPrice()).collect(Collectors.toSet());
//1. sort Product by price
List<ProductCollect> sortByPrice = productsList.stream()
.sorted(Comparator.comparing(ProductCollect::getPrice).reversed()).collect(Collectors.toList());
sortByPrice.forEach(l->System.out.println(l.getPrice()));
}
}