java – Hashtable的值不会增加
发布时间:2020-12-15 04:22:55 所属栏目:Java 来源:网络整理
导读:以下 Java代码: public class TestCSVDataToMap { public static HashtableString,Integer testTable = new Hashtable(); public static void main (String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("test
以下
Java代码:
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); } } 有输出: dash! dash! dash! dash! {0=1} 为什么在解析.csv文件时密钥’0’的值没有增长?在testTable(Hashtable)中,它用(0,1)初始化,并且值应该保持增长,因为符号总是被检测为’0’的键. 解决方法
您正在使用
contains ,它确定参数是否作为Hashtable中的值存在,而不是作为键.因为没有找到,所以你会一遍又一遍.
改为使用 if(testTable.containsKey(symbol)){ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |