Skip to content

Instantly share code, notes, and snippets.

@stuart-marks
Created November 10, 2018 06:29
Show Gist options
  • Save stuart-marks/1593112077f1e67007533165884c7e23 to your computer and use it in GitHub Desktop.
Save stuart-marks/1593112077f1e67007533165884c7e23 to your computer and use it in GitHub Desktop.
data and utilities for Lambda/Streams Master Class
package masterclass;
import java.util.*;
import java.util.stream.*;
import java.util.Map.Entry;
import static java.util.Map.entry;
import static java.util.stream.Collectors.*;
public class MasterClass {
// MAIN
public static void main(String[] args) {
printMap(alphabetMap);
}
// DATA
public static final List<String> alphabet = List.of(
"alfa", "bravo", "charlie", "delta", "echo",
"foxtrot", "golf", "hotel", "india", "juliet",
"kilo", "lima", "mike", "november", "oscar",
"papa", "quebec", "romeo", "sierra", "tango",
"uniform", "victor", "whiskey", "x-ray", "yankee", "zulu");
public static final List<String> sonnet = List.of(
"From fairest creatures we desire increase,",
"That thereby beauty's rose might never die,",
"But as the riper should by time decease,",
"His tender heir might bear his memory:",
"But thou contracted to thine own bright eyes,",
"Feed'st thy light's flame with self-substantial fuel,",
"Making a famine where abundance lies,",
"Thy self thy foe, to thy sweet self too cruel:",
"Thou that art now the world's fresh ornament,",
"And only herald to the gaudy spring,",
"Within thine own bud buriest thy content,",
"And, tender churl, mak'st waste in niggarding:",
"Pity the world, or else this glutton be,",
"To eat the world's due, by the grave and thee.");
// a = alfa, b = bravo, c = charlie, ...
public static final Map<String, String> alphabetMap =
alphabet.stream()
.collect(toMap(s -> s.substring(0, 1), s -> s));
// HELPER METHODS
public static List<String> expand(String s) {
return s.chars()
.mapToObj(Character::toString)
.collect(toList());
}
public static void printMap(Map<?,?> map) {
map.forEach((k, v) -> System.out.printf("%s => %s%n", k, v));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment