fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Map.Entry;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. class Ideone {
  9. public static void main(String[] args) {
  10. test("I like visiting my friend Will, who lives in Orlando, Florida.");
  11. }
  12. private static void test(String input) {
  13. Map<Integer, AtomicInteger> wordCounts = new HashMap<>();
  14. for (Matcher m = Pattern.compile("\\p{L}+").matcher(input); m.find(); ) {
  15. m.group().codePoints().map(Character::toUpperCase).distinct().forEach(ch ->
  16. wordCounts.computeIfAbsent(ch, c -> new AtomicInteger()).incrementAndGet());
  17. }
  18. if (wordCounts.isEmpty())
  19. throw new IllegalArgumentException("No words in input: " + input);
  20. Entry<Integer, AtomicInteger> max = wordCounts.entrySet().stream().
  21. reduce((e1, e2) -> e1.getValue().get() >= e2.getValue().get() ? e1 : e2).get();
  22. System.out.printf("%s %d%n", new String(Character.toChars(max.getKey())), max.getValue().get());
  23. }
  24. }
Success #stdin #stdout 0.1s 711680KB
stdin
Standard input is empty
stdout
I 8