The value of Java – hashtable will not increase

The following java code:

public class TestCSVDataToMap {

    public static Hashtable<String,Integer> testTable = new Hashtable<>();

    public static void main (String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("test.csv"));
        String line;
        while ((line = reader.readLine()) != null) {
            String symbol = "0";
            if(testTable.contains(symbol)) {
                int value = testTable.get(symbol);
                value++;
                testTable.put(symbol,value);
            }
            else {
                System.out.println("dash!");
                testTable.put(symbol,1);
            }
        }
        System.out.println(testTable);
    }
}

With output:

dash!
dash!
dash!
dash!
{0=1}

Why are you parsing The value of key '0' does not increase when the CSV file is? In TestTable (hashtable), it is initialized with (0,1), and the value should keep growing, because the symbol is always detected as the key of '0'

Solution

You are using contains, which determines whether the parameter exists as a value in the hashtable, not as a key Because you can't find it, you will do it again and again

Instead, use containskey, which determines whether the parameter exists as a key

if(testTable.containsKey(symbol)){
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>