-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterF.java
More file actions
59 lines (48 loc) · 1.56 KB
/
InterF.java
File metadata and controls
59 lines (48 loc) · 1.56 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
// EMD-InterF/InterF.java
import java.util.Arrays;
class Person {
private String name;
private int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
public String getName() { return name; }
public int getYear() { return year; }
@Override
public String toString() {
return name + "(" + year + ")";
}
static void show(Person[] persons, String message) {
System.out.println(message);
for (Person person : persons)
System.out.print(person + " ");
System.out.println();
}
}
public class InterF {
public static void main(String[] args) {
Person[] persons =
{ new Person("Mary",1990),
new Person("Joan",1992),
new Person("Suzy",1992),
new Person("Beth",1992),
new Person("Suzy",1980),
new Person("Katy",1982), };
Person.show(persons,"At the beginning:");
// lambda as a single expression -
// no return, no semicolon
Arrays.sort(persons,
(p1, p2) -> p1.getYear()-p2.getYear());
Person.show(persons, "Ordered by age");
// lambda as a compound statement -
// return and semicolons, as usually
Arrays.sort(persons, (p1, p2) ->
{
int d = p1.getName().compareTo(p2.getName());
if (d != 0) return d;
return p1.getYear() - p2.getYear();
});
Person.show(persons,"Ordered by name then age");
}
}