Well, I am having two keys and 3 values for that. Say key1,key2,value1,value2,value3. In future may the values can be increased like value4, value5 so on.

I need to get the values(value1,value2,value3) either by key1 or key2. The key1 will be unique, and key2 is not.

Currently, I am using ENUM for this configuration as like below

public enum EnumClass { Object1(Key1,Key2,Value1,Value2,Value3); }

There will be more than 100 objects. As of now, I am using this ENUM class with 2 maps Map<key1,ENUM> and Map<key2,ENUM>

Is there any other solution that is better than the current solution?

  • 5
    It's hard to tell what you're asking...but are you trying to make mapLikeObject.get(key1) and mapLikeObject.get(key2) both return a collection of three values? Why not just map from keys to a container of values?
    – Ixrec
    Commented Apr 15, 2016 at 9:44
  • 1
    One or two concrete examples (use cases) would be helpful to understanding how you expect to use the keys to retrieve values.
    – Jay Elston
    Commented Apr 16, 2016 at 18:36
  • The two keys in your set are redundant to the keys of your maps. Is this really necessary? Note that you can get key sets of key-value tuples from a Map (if you need to have the key "at hand"). Commented Apr 17, 2016 at 10:15

2 Answers 2


If key2 is not unique, you can't use a Map to store a key2 -> (value1,value2,value3) relationship, since a Map asserts a 1-1 key/value relationship.

I would not worry about performance initially, since you have 100's of objects (as opposed to millions), and I would prefer simplicity/readability to begin with. Hide your lookup behind an interface (so that you can change your implementation later if you wish/need), and perhaps simply store a table of key1/key2/values, and perform your lookup against that simply by iterating through and building a list of matches.

Your repository methods would look something like:

ResultType byKey1(Key k);
Collection<ResultType> byKey2(Key k);

If key1 and key2 are distinct, you could implement a faster lookup by doing something like:


so a lookup by key1 would give you a list of one (value1/value2/value3), whereas a lookup by key2 would give you a list of 'n' (value1/value2/value3). That would require a more complex initial data setup however. Hence I would suggest an initial simple table implementation behind a facade. You can implement optimisations later as required.


You could consider the use of Guava's Table. The instances of this interface create a mapping between two elements and a third one. In your case, it would be a map between (Key1,Key2) and List (or a Value object, depending on the meaning of the extra values. A typical implementation is the HashBasedTable, which is unsurprisingly based on the key hashcodes.

This class only works if Key1 and Key2, together, are distincts. But anyway, if it's not the case, why do you call them keys?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.