0

I have this two-list hashtable:

Hashtable<List<String>, List<String>> aliases = new Hashtable<>();

aliases.put(List.of("name1", "name2"), List.of("John", "15"));
aliases.put(List.of("name3", "name4"), List.of("William", "23"));

String args = "name1";

I'd want to see if args variable exists in the list and get it

henriicoo
  • 5
  • 3
  • You could iterate through the hashtable and check each key if it `contains` your `args` value, but you're using losing the benefit of a hashtable by using it this way. – khelwood Nov 13 '20 at 13:05
  • Could you elaborate on how you intent to use this table? I cannot think of a use case where using a List as hash-key is useful. – MDK Nov 13 '20 at 13:08
  • @MDK it's for a discord bot: aliases.put(List.of("commandname", "commandname2"), List.of("commanddescription", "usage")); but idk if its the best way to do this – henriicoo Nov 13 '20 at 13:20
  • If your main usage will be to look up what alias points to what command it would probably be best to map each alias separately. Then a simple `get(alias)` will find the correct command. If you commands only ever consists of the name and usage, wrapping them into a class with these to attributes will probably be more useful than using a List. – MDK Nov 13 '20 at 13:27

1 Answers1

1

Honestly, I wouldn't recommend using a hash table this way. The benefit from using it is all about finding existing values with O(1).

Of course you can iterate through every single index of the hash table and use "if args contains list" but it will be pointless and expensive. It's the same as not having a hash table at all.

TS_
  • 319
  • 1
  • 5