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 8107441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:46:22+00:00 2026-06-06T00:46:22+00:00

Please advise.. In the following code(Tested. Will work fine) public class Exp_Test { public

  • 0

Please advise.. In the following code(Tested. Will work fine)

public class Exp_Test { 
     public static void main (String[] args) {
        ArrayList<Hashtable> objArrlistHshTbl = new ArrayList<Hashtable>();

        Hashtable objHashTable = new Hashtable();

        objHashTable.put("Key1", "Value1");
        objHashTable.put("Key2", "Value2");

        objArrlistHshTbl.add(objHashTable);

        objHashTable.clear();

        objHashTable.put("Key3", "Value3");
        objHashTable.put("Key4", "Value4");

        objArrlistHshTbl.add(objHashTable);
        System.out.println("Hi");//put a breakpoint here to check the values
    }
}

I am expecting to see an arraylist with to Hashtables saved as array elements, first one with keys ‘Key1’ and ‘Key2’, Second one with Keys ‘Key3’ and ‘Key4’. I get an array list with two hash tables, but values and keys inside hash table are coming as ‘Key3’ and ‘key4’ for both tables. Could someone explain. If it is a reference problem, how do I get two hash tables in to different array elements withouot creating multiple hash tables(I have to run the code inside a for loop to add multiple hash tables.Creating separate hash tables every time does not sound good)

  • 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-06T00:46:24+00:00Added an answer on June 6, 2026 at 12:46 am

    Solved

    public static void main (String[] args) {
        ArrayList<Hashtable> objArrlistHshTbl = new ArrayList<Hashtable>();
        for(int i= 1; i< 3; i++){
                  if(i == 1){
                  Hashtable objHashTable = new Hashtable();
        objHashTable.put("Key" + i, "Value1");
        objHashTable.put("Key" + i+1, "Value2");
                  objArrlistHshTbl.add(objHashTable);
                  }else{
                      Hashtable objHashTable = new Hashtable();
                      objHashTable.put("Key" + i, "Value3");
            objHashTable.put("Key" + i+1, "Value4");
                      objArrlistHshTbl.add(objHashTable);
                  }
    
        }
    
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can someone advise me why the following code appears to work fine in
Getting a segmentation fault for the following code. Please advise. struct columns { char*
Please advise me, what wrong with my following code: <a href=<?php echo $_url; ?>
Looking at the following code snippet : public JsonResult GetMapList(string siteDescription,string siteStatus) { var
Based on the following code, please advise NSString *str= [[NSString alloc] initWithString:@Hello world]; NSLog(@Length:
Can anyone please advise where i'm going wrong with the following? I want the
Can anyone please advise me if there will be any problems changing the wordpress
Can anyone please identify is there any possible memory leaks in following code. I
I've a simple code as following: <html> <body> <div id=div1> <input class=input1 type=text value=click
I am using following code to detach my DB from SQL Server express. Please

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.