-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathArraysSort.java
More file actions
executable file
·39 lines (29 loc) · 859 Bytes
/
Copy pathArraysSort.java
File metadata and controls
executable file
·39 lines (29 loc) · 859 Bytes
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
package com.programcreek.java8.lambda;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Stream;
public class ArraysSort {
public static void main(String[] args) {
Dog d1 = new Dog("Max", 2, 50);
Dog d2 = new Dog("Rocky", 1, 30);
Dog d3 = new Dog("Bear", 3, 40);
Dog[] dogArray = { d1, d2, d3 };
printDogs(dogArray);
Arrays.sort(dogArray, new Comparator<Dog>() {
@Override
public int compare(Dog o1, Dog o2) {
return o1.height - o2.height;
}
});
printDogs(dogArray);
//one line of lambda
Arrays.sort(dogArray, (m, n) -> Integer.compare(m.getWeight(), n.getWeight()));
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs) {
System.out.println("--Dog List--");
for (Dog d : dogs)
System.out.print(d);
System.out.println();
}
}