Skip to content

Instantly share code, notes, and snippets.

@aVolpe
Created April 20, 2016 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aVolpe/0ee564c63f6b80722a03a3b8f5bc2d1b to your computer and use it in GitHub Desktop.
Save aVolpe/0ee564c63f6b80722a03a3b8f5bc2d1b to your computer and use it in GitHub Desktop.
Desafio Owl
package test;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class TestCounter {
public static void main(String[] args) {
String string = "La verdadera ignorancia no es la ausencia de conocimientos… sino el hecho de rehusarse a adquirirlos";
String toSearch = "owl";
System.out.println(groupByLetter(string, toSearch));
}
public static Map<Character, Long> groupByLetter(String string, String toSearch) {
Map<Character, Long> toRet = string.chars()
.mapToObj(TestCounter::toChar)
.filter(c -> toSearch.indexOf(c) >= 0L)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
toSearch
.chars()
.mapToObj(TestCounter::toChar)
.forEach(e -> toRet.putIfAbsent(e, 0L));
return toRet;
}
private static Character toChar(int i) {
return Character.toChars(i)[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment