Reputation: 2368
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);
}
}
has the output:
dash!
dash!
dash!
dash!
{0=1}
Why didn't the value of key '0' grow when the .csv files were parsed? In the testTable(a Hashtable), it is initialized with (0,1), and the value should keep growing, since the symbol is always detected as a key of '0'.
Upvotes: 3
Views: 73
Reputation: 4328
contains
checks for HashTable value. In this case you want to check keys so use for containsKey
.
Upvotes: 0
Reputation: 178293
You are using contains
, which determines if the argument exists as a value in the Hashtable
, not as a key. Because it's not found, you are put
ting 1
over and over.
Use containsKey
instead, which determines if the argument exists as a key.
if(testTable.containsKey(symbol)){
Upvotes: 7