FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_upgrade/src/main/java/sorting/SortGolfers.java at master · sairam-github/java_upgrade · GitHub
sairam-github
/
java_upgrade
Public
forked from
kousen/java_upgrade
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
sorting
/
SortGolfers.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
97 lines (77 loc) · 3.24 KB
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
sorting
/
SortGolfers.java
Copy path
File metadata and controls
97 lines (77 loc) · 3.24 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
85
86
87
88
89
90
91
92
93
94
95
96
97
package
sorting
;
import
java
.
util
.*;
import
java
.
util
.
stream
.
Collectors
;
import
static
java
.
util
.
Comparator
.
comparing
;
import
static
java
.
util
.
Comparator
.
comparingInt
;
public
class
SortGolfers
{
private
List
<
Integer
>
integers
=
Arrays
.
asList
(
3
,
1
,
4
,
1
,
5
,
9
);
public
List
<
Integer
>
getValuesBefore
() {
List
<
Integer
>
results
=
new
ArrayList
<>();
for
(
Integer
i
:
integers
) {
int
val
=
i
*
2
;
if
(
val
%
3
==
0
) {
results
.
add
(
i
);
}
}
return
results
;
}
public
List
<
Integer
>
getValuesAfter
() {
return
integers
.
stream
()
.
mapToInt
(
n
->
n
*
2
)
.
filter
(
n
->
n
%
3
==
0
)
.
boxed
()
.
collect
(
Collectors
.
toList
());
}
private
static
List
<
Golfer
>
golfers
=
Arrays
.
asList
(
new
Golfer
(
"Jack"
,
"Nicklaus"
,
68
),
new
Golfer
(
"Tiger"
,
"Woods"
,
70
),
new
Golfer
(
"Tom"
,
"Watson"
,
70
),
new
Golfer
(
"Ty"
,
"Webb"
,
68
),
new
Golfer
(
"Bubba"
,
"Watson"
,
70
));
public
static
void
printSorted
(
Comparator
<
Golfer
>
comparator
) {
System
.
out
.
println
(
"----------"
);
golfers
.
stream
()
.
sorted
(
comparator
)
.
forEach
(
System
.
out
::
println
);
}
public
static
void
main
(
String
[]
args
) {
Golfer
nicklaus
=
golfers
.
get
(
0
);
System
.
out
.
println
(
nicklaus
);
// sort by score
SortGolfers
.
printSorted
(
comparingInt
(
Golfer
::
getScore
));
// sort by score, descending
SortGolfers
.
printSorted
(
comparingInt
(
Golfer
::
getScore
).
reversed
());
// sort by score, then last name
SortGolfers
.
printSorted
(
comparingInt
(
Golfer
::
getScore
)
.
thenComparing
(
Golfer
::
getLast
));
// sort by score, then last name, then first name
SortGolfers
.
printSorted
(
comparingInt
(
Golfer
::
getScore
)
.
thenComparing
(
Golfer
::
getLast
)
.
thenComparing
(
Golfer
::
getFirst
));
List
<
Golfer
>
sorted
=
golfers
.
stream
()
.
sorted
(
comparing
(
Golfer
::
getScore
)
.
thenComparing
(
Golfer
::
getLast
)
.
thenComparing
(
Golfer
::
getFirst
))
.
collect
(
Collectors
.
toList
());
// The sort still maintains references to the same objects
System
.
out
.
println
(
sorted
.
get
(
0
) ==
nicklaus
);
// Copy, then sort the copies
System
.
out
.
println
(
"Sorting the copies:"
);
sorted
=
golfers
.
stream
()
.
map
(
Golfer
::
new
)
// Make new golfers that are copies of the originals
.
sorted
(
comparing
(
Golfer
::
getScore
)
.
thenComparing
(
Golfer
::
getLast
)
.
thenComparing
(
Golfer
::
getFirst
))
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
sorted
.
get
(
0
) ==
nicklaus
);
// Partition Golfers into two categories: above and below score = 70
Map
<
Boolean
,
List
<
Golfer
>>
map
=
golfers
.
stream
()
.
collect
(
Collectors
.
partitioningBy
(
g
->
g
.
getScore
() <
70
));
System
.
out
.
println
(
map
);
System
.
out
.
println
(
"Printing the partitioned map:"
);
map
.
forEach
( (
key
,
golferList
) -> {
System
.
out
.
println
(
key
);
golferList
.
forEach
(
System
.
out
::
println
);
});
}
}
Back
|
FazBrowse Home
|
New Git URL