Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class WordsForLetter {
- public static void main(String[] args) {
- String input = "ala ma, kota";
- Map<Character, List<String>> resullt = new HashMap<>();
- List<Character> keys = getKeys(input);
- String[] splitedInput = splitInputIntoWords(input);
- createWordsListForKey(resullt, keys, splitedInput);
- printResult(resullt);
- }
- private static void printResult(Map<Character, List<String>> result) {
- result.forEach((key, value) -> System.out.println(
- key + ":" + value));
- }
- private static void createWordsListForKey(Map<Character, List<String>> result, List<Character> keys, String[] s) {
- for (Character key : keys) {
- List<String> words = new ArrayList<>();
- for (String str : s) {
- if (str.indexOf(key) != -1) {
- result.get(key).add(str);
- }
- }
- result.put(key, words);
- }
- }
- private static String[] splitInputIntoWords(String input) {
- return input.split(" ");
- }
- private static List<Character> getKeys(String input) {
- return input.chars()
- .filter(Character::isLetter)
- .distinct()
- .mapToObj(intToChar -> (char) intToChar)
- .collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement