MCPcopy Create free account
hub / github.com/amigoscode/java-streams / Sorting

Class Sorting

src/test/java/com/amigoscode/examples/Sorting.java:13–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11import java.util.stream.Collectors;
12
13public class Sorting {
14
15 @Test
16 public void sortingSteamOfElements() throws IOException {
17 List<Person> people = MockData.getPeople();
18 List<String> sorted = people.stream()
19 .map(Person::getFirstName)
20 .sorted()
21 .collect(Collectors.toList());
22 sorted.forEach(System.out::println);
23 }
24
25 @Test
26 public void sortingSteamOfElementsReverse() throws IOException {
27 List<Person> people = MockData.getPeople();
28
29 List<String> sorted = people.stream()
30 .map(Person::getFirstName)
31 .sorted(Comparator.reverseOrder())
32 .collect(Collectors.toList());
33 sorted.forEach(System.out::println);
34 }
35
36 @Test
37 public void sortingSteamOfObjets() throws IOException {
38 List<Person> people = MockData.getPeople();
39
40 Comparator<Person> comparing = Comparator
41 .comparing(Person::getEmail)
42 .reversed()
43 .thenComparing(Person::getFirstName);
44
45 List<Person> sort = people.stream()
46 .sorted(comparing)
47 .collect(Collectors.toList());
48 sort.forEach(System.out::println);
49 }
50
51 @Test
52 public void topTenMostExpensiveBlueCars() throws IOException {
53 List<Car> cars = MockData.getCars();
54 List<Car> topTen = cars.stream()
55 .filter(car -> car.getColor().equalsIgnoreCase("blue"))
56 .sorted(Comparator.comparing(Car::getPrice).reversed())
57 .limit(10)
58 .collect(Collectors.toList());
59 topTen.forEach(System.out::println);
60 }
61
62}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected