Created
April 20, 2016 22:18
-
-
Save aVolpe/0ee564c63f6b80722a03a3b8f5bc2d1b to your computer and use it in GitHub Desktop.
Desafio Owl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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