views:

86

answers:

2

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?

+2  A: 

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

polygenelubricants
It would figure that as soon as I post this I connect the dots. I'm coming back to this after 4 months of not looking at this code. I had a little snippet of code hidden in user that basically did this: setRoomPseudo(Room r) { if(myRoom != null) myRoom.removeMe() myRoom = r } So, I forgot it worked like that and did operations out of order. fixed, and sorry for the trouble. I do understand maps afterall, and java, just not myself!
Kirk
A: 

Does your code even compile?

public class Library {

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

  public static addRoom(String s, Room r) { // Your hashtable holds Rooms yet you add a Room
    myRooms.put(s, r);
  }

  public static Room getRoomById(String s) { // Again, returning a Room, instead of Rooms
    return myRooms.get(s);
  }
}

Just by looking at this, it shouldn't compile. Now I can only assume that it was a typo, and if that's the case then show us the Room code where you're trying to add a user to the room.

Lirik
It would compile if `Room extends/implements Rooms`. It is peculiar, though.
polygenelubricants
It would figure that as soon as I post this I connect the dots. I'm coming back to this after 4 months of not looking at this code. I had a little snippet of code hidden in user that basically did this: setRoomPseudo(Room r) { if(myRoom != null) myRoom.removeMe() myRoom = r } So, I forgot it worked like that and did operations out of order. fixed, and sorry for the trouble. I do understand maps afterall, and java, just not myself!
Kirk