Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class WordsForLetter {
  5.  
  6.     public static void main(String[] args) {
  7.         String input = "ala ma, kota";
  8.         Map<Character, List<String>> resullt = new HashMap<>();
  9.         List<Character> keys = getKeys(input);
  10.         String[] splitedInput = splitInputIntoWords(input);
  11.         createWordsListForKey(resullt, keys, splitedInput);
  12.         printResult(resullt);
  13.     }
  14.  
  15.     private static void printResult(Map<Character, List<String>> result) {
  16.         result.forEach((key, value) -> System.out.println(
  17.                 key + ":" + value));
  18.     }
  19.  
  20.     private static void createWordsListForKey(Map<Character, List<String>> result, List<Character> keys, String[] s) {
  21.         for (Character key : keys) {
  22.             List<String> words = new ArrayList<>();
  23.             for (String str : s) {
  24.                 if (str.indexOf(key) != -1) {
  25.                     result.get(key).add(str);
  26.                 }
  27.             }
  28.             result.put(key, words);
  29.         }
  30.     }
  31.  
  32.     private static String[] splitInputIntoWords(String input) {
  33.         return input.split(" ");
  34.     }
  35.  
  36.     private static List<Character> getKeys(String input) {
  37.         return input.chars()
  38.                 .filter(Character::isLetter)
  39.                 .distinct()
  40.                 .mapToObj(intToChar -> (char) intToChar)
  41.                 .collect(Collectors.toList());
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement