forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSort2.java
More file actions
70 lines (51 loc) · 1.72 KB
/
ListSort2.java
File metadata and controls
70 lines (51 loc) · 1.72 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
66
67
68
69
70
package com.zetcode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
class Country {
private String name;
private int population;
public Country(String name, int population) {
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
return "Country{" + "name=" + name
+ ", population=" + population + '}';
}
}
public class ListSort2 {
public static void main(String[] args) {
List<Country> countries = createList();
// List<Country> sorted_countries = countries.stream()
// .sorted((e1, e2) -> Integer.compare(e1.getPopulation(),
// e2.getPopulation())).collect(Collectors.toList());
List<Country> sorted_countries = countries.stream()
.sorted(Comparator.comparingInt(Country::getPopulation))
.collect(Collectors.toList());
System.out.println(sorted_countries);
}
private static List<Country> createList() {
List<Country> countries = new ArrayList<>();
countries.add(new Country("Slovakia", 5424000));
countries.add(new Country("Hungary", 9845000));
countries.add(new Country("Poland", 38485000));
countries.add(new Country("Germany", 81084000));
countries.add(new Country("Latvia", 1978000));
return countries;
}
}