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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:37:31+00:00 2026-05-14T23:37:31+00:00

Scenario : Creating a server which has Room objects which contain User objects. I

  • 0

Scenario: Creating a server which has Room objects which contain User objects.

I want to store the rooms in a Map of some sort by Id (a string).

Desired Behavior:

When a user makes a request via the server, I should be able to look up the Room by id from the library and then add the user to the room, if that’s what the request needs.

Currently I use the static function in my Library.java class where the Map is stored to retrieve Rooms:

public class Library {

  private static Hashtable<String, Rooms> myRooms = new Hashtable<String, Rooms>();

  public static addRoom(String s, Room r) {  
    myRooms.put(s, r);
  }

  public static Room getRoomById(String s) {
    return myRooms.get(s);
  }
}

In another class I’ll do the equivalent of myRoom.addUser(user);

What I’m observing using Hashtable, is that no matter how many times I add a user to the Room returned by getRoomById, the user is not in the room later.

I thought that in Java, the object that was returned was essentially a reference to the data, the same object that was in the Hashtable with the same references; but, it isn’t behaving like that. Is there a way to get this behavior? Maybe with a wrapper of some sort? Am I just using the wrong variant of map?

Help?

  • 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-14T23:37:32+00:00Added an answer on May 14, 2026 at 11:37 pm

    It’s very peculiar that you declare myRooms as Hashtable<String, Rooms> (plural Rooms), but you put a singular Room r in addRoom.

    Are you trying to do some sort of multimap, where a key can map to a multiple values? If so, then either us the Guava implementation, or implement your own as a Map<K,Set<V>> (map a key to a set of values).

    I’m not sure if this your fundamental issue, though.

    It is correct that the returned value by get should be the same object, as defined by reference equality, as the one used in put.

        Object someObject = new Object();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key1", someObject);
        System.out.println(map.get("key1") == someObject); // prints "true"
    
        someObject = "something else";
        System.out.println(map.get("key1") == someObject); // prints "false"
    

    The above is expected behavior.

    Sometimes people have trouble with a Map because their key objects do not implement hashCode and equals contract properly, but if you’re using String as keys, this should not be an issue.

    By the way, Hashtable has a newer, sexier cousin in HashMap. I noticed the multithreading tag in the question, so if you actually need the synchronized feature, you can use Collections.synchronizedMap

    In either case, whether you use a HashMap or a Hashtable, you want declare myRooms simply as a Map (see Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces).

    Related questions

    • Overriding equals and hashCode in Java
    • Understanding the workings of equals and hashCode in a HashMap
    • Java hashmap vs hashtable
    • Difference between a Deprecated and Legacy API?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would suggest to use Spring-BlazeDs integration project. One of… May 15, 2026 at 4:39 pm
  • Editorial Team
    Editorial Team added an answer The syntax is valid in some places but doesn't mean… May 15, 2026 at 4:39 pm
  • Editorial Team
    Editorial Team added an answer I suspect the problem is that the web server isn't… May 15, 2026 at 4:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.