forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodReferenceEx.java
More file actions
61 lines (44 loc) · 1.41 KB
/
MethodReferenceEx.java
File metadata and controls
61 lines (44 loc) · 1.41 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
package com.zetcode;
import java.util.Arrays;
import java.util.Comparator;
// Sorting list of objects by their double fields
class Company {
private String name;
private Double rating;
public Company(String name, Double rating) {
this.name = name;
this.rating = rating;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
@Override
public String toString() {
final var sb = new StringBuilder("Company{");
sb.append("name='").append(name).append('\'');
sb.append(", rating=").append(rating);
sb.append('}');
return sb.toString();
}
}
public class MethodReferenceEx {
public static void main(String[] args) {
var companies = Arrays.asList(new Company("Comp A", 4.5),
new Company("Comp B", 6.5), new Company("Comp C", 3.1),
new Company("Comp D", 8.4), new Company("Comp E", 7.8));
companies.sort(Comparator.comparingDouble(Company::getRating));
companies.forEach(System.out::println);
System.out.println("Reversed");
companies.sort(Comparator.comparingDouble(Company::getRating).reversed());
companies.forEach(System.out::println);
}
}