简体   繁体   中英

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.

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.


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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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