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

  • SEARCH
  • Home
  • 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 8825391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:51:58+00:00 2026-06-14T06:51:58+00:00

I am studying java and I am having trouble using put to add data

  • 0

I am studying java and I am having trouble using put to add data into a Java hashtable.

I have this code

double finalIDF = 0.0;
double finalIDF = 0.0;
double finalBM = 0.0;

ArrayList<Double> finalTMlist = new ArrayList<Double>();
 Hashtable<String, ArrayList<Double>> BM25TFIDF = new Hashtable<String, ArrayList<Double>>();
 String[] bm25QueryList // this is the array to store strings like {hey , good , by}

 for(int finalindex = 0; finalindex < bm25QueryList.length ; finalindex++)
{
actualTFvalueforEachDoc.clear();
finalTMlist.clear();
finalIDF = 0.0;
finalIDF = htBM25IDF.get(bm25QueryList[finalindex]);
finalTMlist = tfForAlldoc.get(bm25QueryList[finalindex]);


 for(int innerfinal = 0 ; innerfinal < finalTMlist.size() ; innerfinal++ ){
 finalTM =finalTMlist.get(innerfinal);
finalBM =  finalIDF * finalTM;
actualTFvalueforEachDoc.add(finalBM); 
finalTM = 0.0;
finalBM = 0.0;   }
 BM25TFIDF.put(bm25QueryList[finalindex], actualTFvalueforEachDoc);
 System.out.println("Right before final step after BM25TFIDF " + BM25TFIDF);  }

I would like to put the ArrayList<Double> into the hashtable using a String key.

The first time through the loop I got the key "orange"

Right before final step after BM25TFIDF {orange=[1.1698113207547172, 1.0508474576271187, 0.8682367918523235, 1.6330439988027539, 0.8938401048492793, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0133729569093612, 0.0]}

which is fine

However, when I insert the second arraylist with second string key "bye" I get

Right before final step after BM25TFIDF {orange=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.238037326690413, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], bye=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.238037326690413, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]}

It is overwriting the first arraylist. I used string array to change the key every time, so it shouldn’t be happen.

I am not really sure the reason that it keeps over writing.

does anyone know the reason ?

  • 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-06-14T06:51:59+00:00Added an answer on June 14, 2026 at 6:51 am

    Don’t do finalTmList.clear() in your for loop. That will clear the list pointed to by the finalTmList, and subsequently for all the reference pointing to that list.

    Since, when you add a list to the Map, you are not adding a copy of your List, rather a copy of your List Reference, that points to the same List. So, any change you make to that list with that reference, or any reference pointing to the list, will be reflected in all the reference.

    You should rather create a new list in your for loop, for each entry of your Map: –

    finalTMlist = new ArrayList<Double>();
    

    Move the above statement inside your first for loop.

    for(int finalindex = 0; finalindex < bm25QueryList.length ; finalindex++) {
        finalTMlist = new ArrayList<Double>();
        // Your rest code.
    

    And follow the same thing for actualTFvalueforEachDoc.clear() also, whatever it is, as I can’t see the declaration in the code.

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

Sidebar

Related Questions

I am studying Data Structures in java and I am having difficulty with using
I recently start studying JAVA, and I find this code: public class Dublicate{ public
I am studying Java concurrency right now. I have a question about synchronized and
I'm studying Java. And i wanted to make the code for checking a 2D
I am studying Java and sorting. I have a question about tracking the index
I was studying method overriding in Java when ai came across the this keyword.
I'm a .net developer and I am studying Swing for Java and I have
Question 1: I am studying Android service and often see code like this: private
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
I'm studying RESTful web service using Java. My environment is using Netbean with GlassFish

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.