FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java8InAction/src/main/java/lambdasinaction/chap3/Sorting.java at master · java8/Java8InAction · GitHub
java8
/
Java8InAction
Public
Notifications
You must be signed in to change notification settings
Fork
2.2k
Star
3.2k
Code
Issues
18
Pull requests
7
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Java8InAction
/
src
/
main
/
java
/
lambdasinaction
/
chap3
/
Sorting.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (65 loc) · 2.54 KB
Breadcrumbs
Java8InAction
/
src
/
main
/
java
/
lambdasinaction
/
chap3
/
Sorting.java
Copy path
File metadata and controls
84 lines (65 loc) · 2.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package
lambdasinaction
.
chap3
;
import
java
.
util
.*;
import
static
java
.
util
.
Comparator
.
comparing
;
public
class
Sorting
{
public
static
void
main
(
String
...
args
){
// 1
List
<
Apple
>
inventory
=
new
ArrayList
<>();
inventory
.
addAll
(
Arrays
.
asList
(
new
Apple
(
80
,
"green"
),
new
Apple
(
155
,
"green"
),
new
Apple
(
120
,
"red"
)));
// [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}]
inventory
.
sort
(
new
AppleComparator
());
System
.
out
.
println
(
inventory
);
// reshuffling things a little
inventory
.
set
(
1
,
new
Apple
(
30
,
"green"
));
// 2
// [Apple{color='green', weight=30}, Apple{color='green', weight=80}, Apple{color='green', weight=155}]
inventory
.
sort
(
new
Comparator
<
Apple
>() {
public
int
compare
(
Apple
a1
,
Apple
a2
){
return
a1
.
getWeight
().
compareTo
(
a2
.
getWeight
());
}});
System
.
out
.
println
(
inventory
);
// reshuffling things a little
inventory
.
set
(
1
,
new
Apple
(
20
,
"red"
));
// 3
// [Apple{color='red', weight=20}, Apple{color='green', weight=30}, Apple{color='green', weight=155}]
inventory
.
sort
((
a1
,
a2
) ->
a1
.
getWeight
().
compareTo
(
a2
.
getWeight
()));
System
.
out
.
println
(
inventory
);
// reshuffling things a little
inventory
.
set
(
1
,
new
Apple
(
10
,
"red"
));
// 4
// [Apple{color='red', weight=10}, Apple{color='red', weight=20}, Apple{color='green', weight=155}]
inventory
.
sort
(
comparing
(
Apple
::
getWeight
));
System
.
out
.
println
(
inventory
);
}
public
static
class
Apple
{
private
Integer
weight
=
0
;
private
String
color
=
""
;
public
Apple
(
Integer
weight
,
String
color
){
this
.
weight
=
weight
;
this
.
color
=
color
;
}
public
Integer
getWeight
() {
return
weight
;
}
public
void
setWeight
(
Integer
weight
) {
this
.
weight
=
weight
;
}
public
String
getColor
() {
return
color
;
}
public
void
setColor
(
String
color
) {
this
.
color
=
color
;
}
public
String
toString
() {
return
"Apple{"
+
"color='"
+
color
+
'\''
+
", weight="
+
weight
+
'}'
;
}
}
static
class
AppleComparator
implements
Comparator
<
Apple
> {
public
int
compare
(
Apple
a1
,
Apple
a2
){
return
a1
.
getWeight
().
compareTo
(
a2
.
getWeight
());
}
}
}
Back
|
FazBrowse Home
|
New Git URL