6. Java 8: Lambda cookbook Flashcards

1
Q

What is the result of the following:

System.out.println(map);

A

{A=1, B=0}

The compute map let’s you compute a mapping for the specified key and its current mapped value (or null if the value isn’t present yet).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the new Map enhancements since Java 8 regarding the computing of values?

A

V compute(K key, BiFunction super K, ? super V, ? extends V> remappingFunction);

V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction);

V computeIfPresent(K key, BiFunction super K, ? super V, ? extends V> remappingFunction);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will be the output of the following?
Map map = new HashMap<>();
map.compute(“A”, (k,v) -> (v==null) ? 0 : v + 1);
System.out.println(map.get(“A”));
map.compute(“A”, (k,v) -> (v==null) ? 0 : v + 1);
System.out.println(map.get(“A”));
map.computeIfAbsent(“A”, k -> 99);
System.out.println(map.get(“A”));
map.computeIfAbsent(“B”, k -> 98);
System.out.println(map.get(“B”));
map.computeIfPresent(“C”, (k,v) -> (v==null) ? 0 : v + 1);
System.out.println(map.get(“C”));
map.computeIfPresent(“B”, (k,v) -> (v==null) ? 0 : v + 1);
System.out.println(map.get(“B”));

A
0
1
1
98
null
99
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you easily loop over the keys and values of a map using java 8? Given the following map:
Map map = new HashMap<>();

A

map.forEach((k,v) -> System.out.println(k + “ - “ + v));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you remove all the elements from a String Collection object that start with a “A”? Given the followning list:
List names = new ArrayList<>():

A

names.removeIf(s -> s.startsWith(“A”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Howo can you make all the elements in a String list lowercase? given the following list.
List names = new ArrayList<>():

A

names.replaceAll(s -> s.toLowerCase());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Java 8 a functioin was added to the Files API to find all Path’s (files or directories) matching a predicate. Use this function to retrieve all files that end with a java extension.

A

Path start = Paths.get(“/home”);
int depth = 5;
Stream javaFiles = Files.find(start, depth, (path, attr) -> String.valueOf(path).endsWith(“.java”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Two methods were added to the Files class to make it easier to read lines from a file. Describe these two nem methods.

A

Stream lines = Files.lines(Paths.of(“./readme.txt”));

List lines2 = Files.readAllLines(Paths.of(“./readme.txt”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to use a Stream to walk over all files and directories relative to a start directory of /home. It should use a maximum of 5 folders deep.

A

try(Stream paths = Files.walk(Paths.get(“/home”, 5)) {
paths.forEach(System.out::println);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between the map and flatMap function?

A

Map function will always accept one element and will result in one element.
flatMap function will always accept one element and will produce 0 or more elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the following code, what is the result:
Map map = new HashMap<>();
map.put(“A”, “A”);
map.merge(“A”, “B”, (v1,v2) -> v1 + v2);

A

map contains a key A with value A,B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you create a IntStream of the values 1,2,3,4,5?

A

IntStream.range(1,6); // the end is exclusive
or
IntStream.rangeClosed(1,5); // the end is inclusive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly