vuoi
o PayPal
tutte le volte che vuoi
NUMBER_OF_LINES("Count the lines",
s -> "There are "+ Long.toString(s.chars() .filter(ch -> ch ==
'\n') .count() + 1) + "
lines\n"), ORDER_WORDS("Order words alphabetically", s ->
Arrays.stream(s.toLowerCase().split(WORDS_SEPARATOR))
.filter(str -> !str.isEmpty())
.distinct()
.sorted()
.collect(joining("\n"))),
COUNT_WORDS("Count words", s ->
Arrays.stream(s.toLowerCase().split(WORDS_SEPARATOR))
.collect( groupingBy(str -> str,
collectingAndThen(counting(), x
-> x))) .entrySet()
.stream()
.filter((entry) -> !entry.getKey().isEmpty())
.sorted((e1,e2)->
e2.getValue().compareTo(e1.getValue())) .map((entry) -> entry.getKey() + " -> "+
entry.getValue()) .collect(joining("\n")));
private final String commandName;
private final Function<String, String> fun;
private Command(final String name,
final Function<String, String> process) {
commandName = name;
fun = process;
}
@Override
public String toString() {
return commandName;
}
public String translate(final String s) {
return fun.apply(s);
}
}