Java Map读取文件存储在Map第一个字母中作为键和值的值

时间:2016-10-11 11:00:49

标签: java android string dictionary

我试图通过Key获取Map中的所有字符串,这是地图中每个单词的第一个字母,但不断删除重复项,我只剩下几个字。

public class Search {

    private static void searchHashForLetterKey() throws FileNotFoundException {
            Map<Character, String> charCount = new HashMap<Character, String>();
            Scanner scanner = new Scanner(new File("words.txt"));

            String word = "and";

            while (scanner.hasNext()) {
                char firstChar = scanner.next().charAt(0);
                charCount.put(firstChar, scanner.next());
            }

            // Print out each of the values.
            for (Map.Entry<Character, String> entry: charCount.entrySet()) {
                char character = entry.getKey();
                String count = entry.getValue();
                System.out.println(character + ": " + count);
            }

            if (charCount.containsKey(word.charAt(0))){
                System.out.println("KEY FOUND");
                if(charCount.containsValue(word.trim())){
                    System.out.println("Word found is: " + word);
                } else {
                    System.out.println("Your word is not found in the example: " + word);
                }
            } else {
                System.out.println("Key Not Found: " + word);
            }

        }

        public static void main(String[] args) {
            System.out.println();

            try {
                searchHashForLetterKey();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    }

该文件是一个word.txt文件,其中包含与此相同的英文单词词典:http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt

这是一个工作示例,但仍然无法按预期仍然无法检查单词的contains()。

public static void searchforWords() throws FileNotFoundException {

    String match = "add";

    List<String> words = new ArrayList(Arrays.asList("cat", "ball", "bat", "cup", "add", "ant"));
    Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
    for(String word: words){
        String firstChar = String.valueOf(word.charAt(0));
        if (map.get(firstChar) == null){
            map.put(firstChar, new ArrayList(Arrays.asList(word)));
        }
        else{
            map.get(firstChar).add(word);
        }
    }

    System.out.println(map + "\n");

    if (map.containsKey(String.valueOf(match.charAt(0)))){
            System.out.println("GOT KEY");
        if (map.containsValue(match)){
            System.out.println("GOT VALUE");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Map每个键只能有一个值。要解决此问题,您可以创建Map<Character, List<String>>而不是Map<Character, String>。 Java 8允许您在一个语句中非常优雅地执行此操作:

Map<Character, List<String>> map =
    Files.lines(Paths.get("wordsEn.txt"))
         .collect(Collectors.groupingBy(s -> s.charAt(0)));

答案 1 :(得分:0)

HashMap定义为key,将ArrayList<String>定义为value,并将所有名称列表存储在起始字符匹配HashMap键中:

private static void searchHashForLetterKey() throws FileNotFoundException {
            Map<Character, ArrayList<String>> charCount = new HashMap<Character, ArrayList<String>>();
            Scanner scanner = new Scanner(new File("words.txt"));

            String word = "and";

            while (scanner.hasNext()) {
                char firstChar = scanner.next().charAt(0);
                ArrayList<String> list;
                if (charCount.containsKey(firstChar)) {
                    list = charCount.get(firstChar);
                } else {
                    list = new ArrayList<>();
                }
                list.add(scanner.next());
                charCount.put(firstChar, list);
            }

            // Print out each of the values.
            for (Map.Entry<Character, ArrayList<String>> entry : charCount.entrySet()) {
                char character = entry.getKey();
                ArrayList<String> list = entry.getValue();
                System.out.println(character + ": " + list.size());
            }

            if (charCount.containsKey(word.charAt(0))) {
                System.out.println("KEY FOUND");
                ArrayList<String> list = charCount.get(word.charAt(0));
                if (list.contains(word.trim())) {
                    System.out.println("Word found is: " + word);
                } else {
                    System.out.println("Your word is not found in the example: " + word);
                }
            } else {
                System.out.println("Key Not Found: " + word);
            }

        }

        public static void main(String[] args) {
            System.out.println();

            try {
                searchHashForLetterKey();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }