Kaz Wolfe Kaz Wolfe - 1 year ago 76
Java Question

Is using a HashMap the simplest solution for storing an Object that has an ID?

I have a class in some code,

(some unneccessary code omitted), that I'm having a bit of trouble with.

public class ChatChannel {

private static HashMap<String, ChatChannel> registeredChannels = new HashMap<>(); // ChannelID, ChatChannel Object

public static void registerChannel(ChatChannel channel) {
registeredChannels.put(channel.getId(), channel);

public static ChatChannel getChannelById(String id) {
return registeredChannels.getOrDefault(id, null);

/** The actual ChatChannel item is defined BELOW THIS LINE **/

private String name;
private String id;

public ChatChannel(String name, String id) {
this.name = name;
this.id = id;

public static String getId() {
return id;

Essentially, this class will allow me to separate messages sent by users into "channels." Users may only receive messages in joined channels, and may only send a message to their active channel. Channels should be accessible using their ID (for example,

However, my problem is I don't know whether I should use a
in order to keep the code light and simple. Ideally, I'd like to be able to reference any
by its
at any point in the code, so I don't need to constantly pass around these
. What, if any, would the performance gain of using
(and external IDs) be? Would it be roughly equal to using a Collection and then iterating through it using my
method? If so, which is considered "proper" Java?

Answer Source

To answer the stated question "Should I be using a HashMap or Collection for performance?" — you can't and won't use a "Collection" in this sense because a Collection is an abstract concept, represented in Java as an interface.

A Collection could be a List, or a Map, or a Set, among other things. You can write a method that, for example, accepts (any kind of) a Collection and performs an operation on everything in the Collection, but in your case here you must decide on what kind of collection to use in your implementation.

Since you're retrieving a channel given an identifier String, a Map is a useful choice because it is a key-to-value mapping; you don't have to iterate through it to find the element that has the desire key.

You should generally declare things generically, then instantiate them with a specific implementation. That is, when working with it in your code you don't care what sort of Map it is, just that it's a Map. The actual map that you allocate could be a HashMap or a LinkedHashMap or a TreeMap — since maintaining the insertion order or keeping things sorted doesn't seem to matter here, the plain HashMap appears appropriate.

private static Map<String, ChatChannel> registeredChannels = new HashMap<>();
//             ^^^ generic declarat   |  specific implementation ^^^^

You might also know something about how many channels there are likely to be, or at least the size of the starting set of channels, so you may also consider the initialCapacity and the loadFactor parameters to the constructor, for example

// Allocate with room for 10 initial channels, expand the map size when 75% full
private static Map<String, ChatChannel> registeredChannels =
        new HashMap<>(10, 0.75);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download