Newer
Older
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
package exercises;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* This set of exercises is about lambdas and method references.
* You will write a lambda or method reference corresponding to
* each of several different functional interfaces. Each exercise
* is named after the functional interface intended to be used
* as the solution.
*/
public class A_LambdasAndMethodReferences {
@Test @Ignore
public void a01predicate() {
// TODO: write a lambda expression that is a predicate
// that tests whether a string is longer than four characters.
Predicate<String> pred = null;
assertTrue(pred.test("abcde"));
assertFalse(pred.test("abcd"));
}
@Test @Ignore
public void a02predicate() {
// TODO: write a lambda expression that is a predicate
// that tests whether a string is empty
Predicate<String> pred = null;
assertTrue(pred.test(""));
assertFalse(pred.test("a"));
}
@Test @Ignore
public void a03predicate() {
// TODO: write a method reference that is a predicate
// that tests whether a string is empty
Predicate<String> pred = null;
assertTrue(pred.test(""));
assertFalse(pred.test("a"));
}
@Test @Ignore
public void a04function() {
// TODO: write a lambda expression that wraps the given
// string in parentheses.
Function<String, String> func = null;
assertEquals("(abc)", func.apply("abc"));
}
@Test @Ignore
public void a05function() {
// TODO: write a lambda expression that converts the
// given string to upper case.
Function<String, String> func = null;
assertEquals("ABC", func.apply("abc"));
}
@Test @Ignore
public void a06function() {
// TODO: write a method reference that converts the
// given string to upper case.
Function<String, String> func = null;
assertEquals("ABC", func.apply("abc"));
}
@Test @Ignore
public void a07consumer() {
// TODO: write a lambda expression that appends the
// string "abc" to the given StringBuilder.
Consumer<StringBuilder> cons = null;
StringBuilder sb = new StringBuilder("xyz");
cons.accept(sb);
assertEquals("xyzabc", sb.toString());
}
@Test @Ignore
public void a08consumer() {
// TODO: write a lambda expression that clears the given list.
Consumer<List<String>> cons = null;
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
cons.accept(list);
assertTrue(list.isEmpty());
}
@Test @Ignore
public void a09consumer() {
// TODO: write a method reference that clears the given list.
Consumer<List<String>> cons = null;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
cons.accept(list);
assertTrue(list.isEmpty());
}
@Test @Ignore
public void a10supplier() {
// TODO: write a lambda expression that returns a new StringBuilder
// containing the string "abc".
Supplier<StringBuilder> sup = () -> new StringBuilder("abc");
assertEquals("abc", sup.get().toString());
}
@Test @Ignore
public void a11supplier() {
// TODO: write a lambda expression that returns a new, empty StringBuilder
Supplier<StringBuilder> sup = null;
assertEquals("", sup.get().toString());
}
@Test @Ignore
public void a12supplier() {
// TODO: write a method reference that returns a new, empty StringBuilder
Supplier<StringBuilder> sup = null;
assertEquals("", sup.get().toString());
}
@Test @Ignore
public void a13runnable() {
StringBuilder sb = new StringBuilder("abc");
String suffix = "xyz";
// TODO: write a lambda expression that appends the 'suffix'
// variable (a String) to the sb variable (a StringBuilder).
Runnable r = () -> sb.append(suffix);
r.run();
r.run();
r.run();
assertEquals("abcxyzxyzxyz", sb.toString());
}
}