FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_upgrade/src/main/java/sorting/SortStrings.java at master · amhtech/java_upgrade · GitHub
amhtech
/
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
/
SortStrings.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (48 loc) · 2.13 KB
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
sorting
/
SortStrings.java
Copy path
File metadata and controls
58 lines (48 loc) · 2.13 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
package
sorting
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
List
;
import
java
.
util
.
stream
.
Collectors
;
public
class
SortStrings
{
public
static
void
main
(
String
[]
args
) {
List
<
String
>
strings
=
Arrays
.
asList
(
"this"
,
"is"
,
"a"
,
"list"
,
"of"
,
"strings"
);
System
.
out
.
println
(
strings
);
System
.
out
.
println
(
"Natural sort:"
);
Collections
.
sort
(
strings
);
System
.
out
.
println
(
strings
);
System
.
out
.
println
(
"Sort by length using a Comparator impl anon inner class:"
);
Collections
.
sort
(
strings
,
new
Comparator
<
String
>() {
@
Override
public
int
compare
(
String
s1
,
String
s2
) {
return
s1
.
length
() -
s2
.
length
();
}
});
System
.
out
.
println
(
strings
);
System
.
out
.
println
(
"Reverse sort by length with a Comparator impl lambda expression:"
);
Collections
.
sort
(
strings
, (
s1
,
s2
) ->
s2
.
length
() -
s1
.
length
());
System
.
out
.
println
(
strings
);
// Stream has a sorted() method that is not destructive
System
.
out
.
println
(
"Natural sort using Stream.sorted()"
);
List
<
String
>
sorted
=
strings
.
stream
()
.
sorted
()
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
sorted
);
System
.
out
.
println
(
"Reverse length sort using Stream.sorted(Comparator)"
);
sorted
=
strings
.
stream
()
.
sorted
((
s1
,
s2
) ->
s2
.
length
() -
s1
.
length
())
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
sorted
);
System
.
out
.
println
(
"Sort by length using Comparator.comparingInt()"
);
sorted
=
strings
.
stream
()
.
sorted
(
Comparator
.
comparingInt
(
String
::
length
))
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
sorted
);
System
.
out
.
println
(
"Sort by length, then equal lengths reverse alpha"
);
sorted
=
strings
.
stream
()
.
sorted
(
Comparator
.
comparingInt
(
String
::
length
)
.
thenComparing
(
Comparator
.
reverseOrder
()))
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
sorted
);
}
}
Back
|
FazBrowse Home
|
New Git URL