6. Java 8: Lambda cookbook Flashcards
What is the result of the following:
System.out.println(map);
{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).
What are the new Map enhancements since Java 8 regarding the computing of values?
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);
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”));
0 1 1 98 null 99
How can you easily loop over the keys and values of a map using java 8? Given the following map:
Map map = new HashMap<>();
map.forEach((k,v) -> System.out.println(k + “ - “ + v));
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<>():
names.removeIf(s -> s.startsWith(“A”));
Howo can you make all the elements in a String list lowercase? given the following list.
List names = new ArrayList<>():
names.replaceAll(s -> s.toLowerCase());
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.
Path start = Paths.get(“/home”);
int depth = 5;
Stream javaFiles = Files.find(start, depth, (path, attr) -> String.valueOf(path).endsWith(“.java”));
Two methods were added to the Files class to make it easier to read lines from a file. Describe these two nem methods.
Stream lines = Files.lines(Paths.of(“./readme.txt”));
List lines2 = Files.readAllLines(Paths.of(“./readme.txt”));
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.
try(Stream paths = Files.walk(Paths.get(“/home”, 5)) {
paths.forEach(System.out::println);
}
What is the difference between the map and flatMap function?
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.
Given the following code, what is the result:
Map map = new HashMap<>();
map.put(“A”, “A”);
map.merge(“A”, “B”, (v1,v2) -> v1 + v2);
map contains a key A with value A,B
How can you create a IntStream of the values 1,2,3,4,5?
IntStream.range(1,6); // the end is exclusive
or
IntStream.rangeClosed(1,5); // the end is inclusive