Scenario : creating a server with Room objects that contains user objects.
I want to save rooms on any map by identifier (string).
The desired behavior:
When a user makes a request through the server, I should be able to search for the room by ID from the library, and then add the user to the room, if necessary for the request.
I am currently using a 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 will make the equivalent of myRoom.addUser(user);
What I am observing with the Hashtable is that no matter how many times I add the user to the Room returned by getRoomById, the user is not in the room later.
I thought that in Java, the returned object was essentially a data reference, the same object that was in the Hashtable with the same references; but he does not behave like that. Is there any way to get this behavior? Maybe with some kind of wrapper? Am I just using the wrong map option?
reference
source share