0

I have a map of values. The values are limited to just 3 possibilities ( "A", "B", "C") . I will have some 100 entries for this map.

Map<String, String> map = new HashMap<>();
map.put("key1","A");
map.put("key2","B");
map.put("key3","C");
map.put("key4","A");
map.put("key5","B");
.
.
.

The only purpose of this map would be a function where I get the key as input and I need to return one of the three "A","B","C" or null if the key has no value. My lookup will only be based on the key here.

I cannot decide if I should repeat the values and construct a flat map as above, or to use a Map<String, List<String>> where the key would be the three values. As in

Map<String, List<String>> map = new HashMap<>();
map.put("A",Arrays.asList("key1","key2"));
map.put("B",Arrays.asList("key3","key4"));
map.put("C",Arrays.asList("key5","key6"));

Or, do I use 3 separate List<String> variables and check using contains each time. As in

List<String> A_LIST = Arrays.asList("key1","key2");
List<String> B_LIST = Arrays.asList("key3","key4");
List<String> C_LIST = Arrays.asList("key5","key6");

Also, do I use an enum/static Strings for the values here and repeat them while constructing the Map? What would be the proper way to do this? Any advice would be useful.

kryf5
  • 1
  • What's the purpose? Memory/time optimization? And what is the approximate size of data (keys & values) stored? – Nikolai Shevchenko Apr 15 '20 at 07:34
  • It's just a normal lookup map. I need to get the corresponding "A","B","C" values from a key which I give to a function. I'm just curious as what would be the better approach to this. The map itself is not very large. Just maybe 200 elements at max, and range of the values is just 3. It is not a performance centric code too. What started my doubt was that if repeating the same key was a norm for this kind of use case ? – kryf5 Apr 15 '20 at 07:38
  • There is no key repetition, only value repetition. If you want you could introduce an enum for those. Other than that, you shouldn't overthink your code. The first version is imo the most readable and probably the most performant at the same time (which doesn't really matter in this case). – Amongalen Apr 15 '20 at 07:58

1 Answers1

0

I'm not sure if I understood your problem. But I think the get(key) method of your map should be enough. If there is no entry in the map it will return null.

I would only change the value so it is an Enum and you can be sure that there are no other values besides A, B and C.

Zevesh
  • 81
  • 1
  • 9
  • Yes, a get would solve my problem. I was wondering if that would be the right way to do it though, since I would have a lot of repetition for the values when I put in the map. is that the right way to do this? Or is there some other standard which is followed? – kryf5 Apr 15 '20 at 07:42
  • @kryf5 if you don't want to use enums or statically declared strings, you can use String.intern() to ensure that excessive memory is NOT allocated. See for details https://stackoverflow.com/a/10579062/2224047 – Nikolai Shevchenko Apr 15 '20 at 07:56