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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:58:25+00:00 2026-06-15T16:58:25+00:00

My add to hashtable method fails, what have i done wrong? Or what have

  • 0

My add to hashtable method fails, what have i done wrong? Or what have i missunderstood?

test:

@Test
public void testAddKeyValue() {
    AdminController cont = new AdminController();

    Apartment o1 = new Apartment(1, 4, "Maier B", true);
    ArrayList<Expense> exp = new ArrayList<>();

    cont.addKeyWithList(o1, exp);
    assertTrue(cont.isEmpty()); // ISSUE > the test works if it is true, but it is supposed be  False.
}

repo class:

public class Repository extends HashMap<Apartment, ArrayList<Expense>>{
    private Map<Apartment,ArrayList<Expense>> dic; // last expense object refers to curret month
    Iterator<Map.Entry<Apartment, ArrayList<Expense>>> it;
    public void addKeyWithList(Apartment apt, ArrayList<Expense> exp){
        dic.put(apt, exp);
        }
}

Why is my test not working? Or where in the code have I done something wrong?

  • 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-15T16:58:26+00:00Added an answer on June 15, 2026 at 4:58 pm

    Don’t extend HashMap as you’re doing. Use a HashMap and delegate to it:

    public class Repository {
        private Map<Apartment, List<Expense>> dic = new HashMap<Apartment, List<Expense>>();
    
        public void addKeyWithList(Apartment apt, ArrayList<Expense> exp){
            dic.put(apt, exp);
        }
    
        public boolean isEmpty() {
            return dic.isEmpty();
        }
    }
    

    At the moment, Repository is a HashMap, but you don’t store anything in it: you store the values in another HashMap contained in Repository.

    Also, storing an iterator in a field is a bad idea. iterators can be used only once. Once they have iterated, the can’t iterate anymore. It should be a local variable.

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

Sidebar

Related Questions

I have a list of string values that I want add to a hashtable
public DataSet ExampleMethod(int param1, string param2, Hashtable ht) { if(ht==null) { ht = new
To add a new value to a dotnet Hashtable I've always used: myHashtable.Add(myNewKey, myNewValue);
I am trying to add an item into my Hashtable, I have used a
I have a method: private List<String> userCns = Collections.synchronizedList(new ArrayList<String>()); private List<String> recipients =
The .add method appends components to the end of another component. I'm trying to
To add a new pair to Hash I do: {:a => 1, :b =>
Say I have a Hashtable<String, Object> with such keys and values: apple => 1
I'm writting a Schedule structure in Java. I have a Hashtable with enum Day
I try to use a HashTable in my webservice.Some of my webservice method prepare

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.