1

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!

1
  • What do you see when you try to debug your program in a debugger? Commented Mar 5, 2012 at 16:59

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!!!!! That fixed it!!! I want to know though, why did temp.clear() affect the arraylist I inserted? Afterall the clear statement was called after I had inserted the arraylist into the hashmap
@user1192724: map.put() doesn't copy the data; it inserts the same reference as temp. So if you change the contents of the temp list post-insertion, the changes get reflected in the map.
Oh I see...That's very useful information. Thank you very much!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.