-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonFifteen.java
More file actions
48 lines (39 loc) · 1.33 KB
/
LessonFifteen.java
File metadata and controls
48 lines (39 loc) · 1.33 KB
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
package newLessons.lessonFifteen;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import newLessons.Lesson;
public class LessonFifteen extends Lesson {
@Override
public void theLesson() {
Function<Object, String> f = LessonFifteen::stringify;
// By saving the method stringify as a lambda of type function the lambda is now the same method as the apply method.
List<String> l = new LinkedList<>();
l.add("You");
l.add("Jack");
l.add("Tewne");
l.add("Lole");
l.add("Menies");
System.out.println(f.apply(l));
System.out.println(f.apply(new String[]{"32", "sdf", "sdffr"}));
Function<String, Integer> function = (s) -> {
return s.chars().sum(); //String.chars returns a stream although I don't do any functional things to it.
};
System.out.println(function.apply("Your mom!"));
}
/**
* A method that will automatically call the correct method for obtaining a string representation of the Object.
*
* @param o The object that will be stringified.
* @return A string representation of the object passed in.
*/
private static String stringify(Object o) {
// TODO Make sure that the string return by toString makes sense (for arrays may have to use Arrays class).
return o.toString();
}
@Override
public void theAssignment() {
Worker w = new Worker();
w.start();
}
}