Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:57:00+00:00 2026-05-30T20:57:00+00:00

I have a hashmap and I am inserting an arraylist as the value in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:57:02+00:00Added an answer on May 30, 2026 at 8:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a hashmap where every key has many values(stored in a arraylist). How
I have HashMap object contains a key x-y-z with corresponding value test-test1-test2 . Map<String,String>
I have a HashMap where the key is a word and the value is
I have a hashmap with some information(key and value) in a perl file. I
I have a HashMap<String,String> (called p2p ) and I want to make a loop
I have a HashMap having key as my own object and key as ArrayList
I have a HashMap where the key is of type String and the value
I have HashMap like: static HashMap<String,ArrayList<Media>> mediaListWithCategory=new HashMap<String,ArrayList<Media>>(); I have value like: January: -Sunday
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have hashmap and its keys are like folder/1.txt,folder/2.txt,folder/3.txt and value has these text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.