I have a HashMap
defined like this
HashMap<String, IConnection> connections = new HashMap<String, IConnection>();
inside application connect
, I add values into it like this:
conn.setAttribute(“username”, username);
connections.put(username, conn); // username and conn are parameters passed to
// connect method
inside application disconnect
method, I remove values from it like this
connections.remove((String)conn.getAttribute(“username”));
This seems to work, however is it correct/safe? Or am I doing it wrong?
Yes, it is alright but I suggest that you make sure the connection is still connected before you try to access or write to it.