Reputation: 529
I have a hashmap and I am inserting an arraylist as the value in a while loop. During an iteration, if the hashmap already contains a key, I want to retreive the arraylist already stored, append new data to it and put it back into the hashmap.
The issue I have now is when I check if a hashmap contains a value and it does, the arraylist being returned is of size 0 as though I never put in anything before.
final HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
String status = "";
String channel = "";
while (daily.next()) {
while (avrg.next())
if (avrg.getString(1).equals(daily.getString(1)) && avrg.getString(2).equals(daily.getString(2))) {
final Object pstmnt = null;
final Object pstmnt2 = null;
final float thresholdValue = calcThreshold(pstmnt, pstmnt2, daily.getString(1));
channel = daily.getString("client_name");
if (daily.getFloat(3) > avrg.getFloat(3) + thresholdValue * avrg.getFloat(3)) {
status = "HIGHER";
}
else if (daily.getFloat(3) < avrg.getFloat(3) - thresholdValue * avrg.getFloat(3)) {
status = "LOWER";
}
else {
status = "Normal";
}
final PrintStream out;
out.println(channel);
out.println(thresholdValue);
out.println(status);
if (map.containsKey(channel)) {
out.println("map contained key");
final ArrayList<String> temp = new ArrayList<String>();
temp.addAll(map.get(channel));
out.println("previous size: " + temp.size());
temp.add(daily.getString("event"));
temp.add(Float.toString(daily.getFloat(3)));
temp.add(Float.toString(avrg.getFloat(3)));
temp.add(Float.toString(thresholdValue * 100));
temp.add(status);
out.println("current size: " + temp.size());
map.put(channel, temp);
out.println("array added to map");
temp.clear();
System.out.println("done");
}
else {
final ArrayList<String> temp = new ArrayList<String>();
out.println("new key created");
temp.add(daily.getString("event"));
temp.add(Float.toString(daily.getFloat(3)));
temp.add(Float.toString(avrg.getFloat(3)));
temp.add(Float.toString(thresholdValue * 100));
temp.add(status);
System.out.println(temp.size());
map.put(channel, temp);
System.out.println("array added to map");
}
}
avrg.beforeFirst();
}
I am really not sure why this is happening. Any help would be appreciated!
Upvotes: 1
Views: 1899
Reputation: 24262
Let me start by saying that @aix has the correct answer, but you should rewrite you code to get rid of all the duplication. The if block starting from
if (map.containsKey(channel))
can be simplified to the following.
ArrayList<String> temp = map.get(channel);
if (temp == null) {
out.println("new key created");
ArrayList<String> temp = new ArrayList<String>();
map.put(channel, temp);
}
temp.add(daily.getString("event"));
temp.add(Float.toString(daily.getFloat(3)));
temp.add(Float.toString(avrg.getFloat(3)));
temp.add(Float.toString(thresholdValue * 100));
temp.add(status);
There is no need to create a new list and copy every time you add to it.
Upvotes: 1
Reputation: 500257
The problem is right here:
map.put(channel, temp);
out.println("array added to map");
temp.clear();
temp.clear()
clears the same list that you've just added to the map. Since temp
is about to go out of scope, that statement serves no useful purpose and can be removed.
Upvotes: 5