简体   繁体   English

迭代 Hashmap 中的值列表

[英]Iterate over a list of values within a Hashmap

Lexical Analysis program: with a text file, segregate each token under categories such as keyword, characters, digits, integers.词法分析程序:用一个文本文件,将每个标记分离到关键字、字符、数字、整数等类别下。

So I created a hashMap where Each key in the hash map have to be segregated in a way that ie Keyword = [if, then, else], Special = [(,),.,;], Character = [a,b,c], so each key has it's own list.所以我创建了一个 hashMap,其中散列图中的每个键都必须以某种方式进行隔离,即 Keyword = [if, then, else], Special = [(,),.,;], Character = [a,b, c],所以每个键都有自己的列表。

But I also need to count the number of times each token appears within the text file, so I was thinking of iterating through each separate list and finding the occurrences of each word/character/digit there, however it's definitely a lot of bad code because I can't think of a way that's better than if list.contains(word) count++, and doing that for every single word/character/digit.但我还需要计算每个标记在文本文件中出现的次数,所以我想遍历每个单独的列表并在那里找到每个单词/字符/数字的出现次数,但是这绝对是很多错误的代码,因为我想不出比 list.contains(word) count++ 更好的方法,并且对每个单词/字符/数字都这样做。


import java.io.*;
import java.util.*;

public class Main {

    private static HashMap<String, ArrayList<String>> map;
    private static ArrayList<String> keys;
    private static ArrayList<String> special;
    private static ArrayList<String> characters;
    private static ArrayList<String> integers;
    private static ArrayList<String> digits;
    private static Token k;

    public static void main(String[] args) {

        map = new HashMap<>();
        keys = new ArrayList<>();
        special = new ArrayList<>();
        characters = new ArrayList<>();
        integers = new ArrayList<>();
        digits = new ArrayList<>();
        k = new Token();

        executeAnalysis();
    }

    private static void executeAnalysis() {
        try {
            File file = new File("/Users/karinaabad/Desktop/HW311.txt");
            Scanner scanner = new Scanner(file);

            while(scanner.hasNext()) {
                String w = scanner.next();
                segregateTokens(w);
            }
            printSummary();
        } catch (FileNotFoundException f) {
            System.out.println("Invalid file input.");
        }
    }

    private static void segregateTokens(String w) {
        if (k.checkKeyword(w))
        {
            keys.add(w);
        }

        if (k.checkSpecial(w)) {
            special.add(w);
        }

        if (k.checkDigit(w)) {
            digits.add(w);
        }

        if (k.checkIntegers(w)) {
            integers.add(w);
        }

        if (k.checkCharacters(w)) {
            characters.add(w);
        }

        map.put("Keyword", keys);
        map.put("Special", special);
        map.put("Digit", digits);
        map.put("Integers", integers);
        map.put("Characters", characters);
    }

    private static void printSummary() {
        System.out.println("==========Summary Report==========");
        for (Map.Entry<String, ArrayList<String>> pair : map.entrySet())
        {
            System.out.println(pair.getKey() + " : " + pair.getValue());
        }
    }

    private static void checkCount() {
        for (Map.Entry<String, ArrayList<String>> key : map.entrySet()) {
            for (String value : )
        }
    }
}

The println() call prints a newline after the loop has completed (at the end of the line containing the integers). println()调用在循环完成后(在包含整数的行的末尾println()打印一个换行符。

If the println() was not there and the code were to try to print something else, it would appear on the same line as the integers.如果println()不存在并且代码尝试打印其他内容,它将与整数出现在同一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM