FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_upgrade/src/test/java/streams/StringExercises.java at main · alcmontejo/java_upgrade · GitHub
alcmontejo
/
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
/
test
/
java
/
streams
/
StringExercises.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (51 loc) · 1.79 KB
Breadcrumbs
java_upgrade
/
src
/
test
/
java
/
streams
/
StringExercises.java
Copy path
File metadata and controls
72 lines (51 loc) · 1.79 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
package
streams
;
import
org
.
junit
.
jupiter
.
api
.
Test
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
List
;
import
java
.
util
.
function
.
Function
;
public
class
StringExercises
{
private
final
List
<
String
>
strings
=
Arrays
.
asList
(
"this"
,
"is"
,
"a"
,
"list"
,
"of"
,
"strings"
);
@
Test
public
void
stringLengthSort_InnerClass
() {
// Java 5, 6, 7
strings
.
sort
(
new
Comparator
<
String
>() {
@
Override
public
int
compare
(
String
s1
,
String
s2
) {
return
s1
.
length
() -
s2
.
length
();
}
});
System
.
out
.
println
(
strings
);
}
@
Test
public
void
stringLengthSort_lambda
() {
// Use lambda for the Comparator (reverse sort)
// Use the "sorted" method on Stream
}
private
int
compareStrings
(
String
s1
,
String
s2
) {
return
s1
.
length
() -
s2
.
length
();
}
@
Test
// Use a lambda that calls 'compareStrings' directly
public
void
stringLengthSort_methodCall
() {
}
@
Test
// Use a method ref to 'compareStrings'
public
void
stringLengthSort_methodRef
() {
}
@
Test
// Use Comparator.comparingInt
public
void
stringLengthSort_comparingInt
() {
}
@
Test
public
void
demoCollectors
() {
// Get only strings of even length
// Add them to a LinkedList
// Add the strings to a map of string to length
// Filter out nulls, then print even-length strings
// Function composition
// Combine the two predicates and use the result to print non-null, even-length strings
// f: A -> B, g: B -> C, (g.f)(x) = g(f(x)), A -> C
}
// generated by GitHub Copilot
private
<
A
,
B
,
C
>
Function
<
A
,
C
>
compose
(
Function
<
A
,
B
>
f
,
Function
<
B
,
C
>
g
) {
return
x
->
g
.
apply
(
f
.
apply
(
x
));
}
}
Back
|
FazBrowse Home
|
New Git URL