Obtain Value from nested Hashtable Map

I am creating a program that initially uses nested Hashtables in the format:
public static Hashtable<Integer, Map<String, String>> commonOpposites = new Hashtable<>(){{...}};
public static Hashtable<Integer, Map<String, String>> commonOpposites = new Hashtable<>(){{...}};
Purpose The functionality of the nested hashtable is to hold values of 'common opposites' (antonyms for clarity). I have already structured most of the code to put values within the hashtable but I am having a difficult time figuring out how to obtain only values within the
key : {key = value}
key : {key = value}
format of the nested hashtable. Here is how I placed values into the table (I put them in pairs of 2 for ease of readability):
public static Hashtable<Integer, Map<String, String>> commonOpposites = new Hashtable<>(){{
put(1, Map.of("absent", "present")); put(2, Map.of("accurate", "inaccurate"));
put(3, Map.of("against", "for")); put(4, Map.of("all", "none"));
...
}};
public static Hashtable<Integer, Map<String, String>> commonOpposites = new Hashtable<>(){{
put(1, Map.of("absent", "present")); put(2, Map.of("accurate", "inaccurate"));
put(3, Map.of("against", "for")); put(4, Map.of("all", "none"));
...
}};
I am not even sure if this is the best way to write code like this, however, I have tried using for loops with Entry, tried using built in functions and looked at examples like:
Map map = new HashMap();
((Map)map.get( "keyname" )).get( "nestedkeyname" );
Map map = new HashMap();
((Map)map.get( "keyname" )).get( "nestedkeyname" );
I am not sure how I would access them, if someone could explain to me the complexities of something like this, it would mean a lot!
11 Replies
JavaBot
JavaBot3y ago
Hey, @verbose! Please remember to /close this post once your question has been answered!
Kyo-chan
Kyo-chan3y ago
I'm afraid there is nothing complex. It's a Map of Maps of Strings. That works as usual. String value = map.get("key").get("subkey"); It may be worth verifying, however, whether you truly want nested maps, rather than a composed key Oh, right, the top-level key is of type Integer. So it would be rather: String value = map.get(2).get("subkey");
verbose
verboseOP3y ago
Ic Ic it does seem very straightforward but I have had some difficulty using it within an if statement basically
if(dict.getCommonOpposites().entrySet().contains(Key)) {
TypeWriter("""
Correct ✔️
""", 20, Colors.GREEN_BOLD);
}else {
TypeWriter("""
Incorrect ❌
""", 20, Colors.RED_BOLD);
}
if(dict.getCommonOpposites().entrySet().contains(Key)) {
TypeWriter("""
Correct ✔️
""", 20, Colors.GREEN_BOLD);
}else {
TypeWriter("""
Incorrect ❌
""", 20, Colors.RED_BOLD);
}
I tried to check whether a user inputs a value that is an antonym of the word I provide them such as
What is the opposite of absent?
User In: present
Correct!

or
What is the opposite of absent?
User In: here
Incorrct, Try again!
What is the opposite of absent?
User In: present
Correct!

or
What is the opposite of absent?
User In: here
Incorrct, Try again!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan3y ago
Looks like nested maps aren't what's needed anyway. More like two-ways Map, which isn't provided by Java, but you can always enter a word and its opposite with both sides
verbose
verboseOP3y ago
initially it was used in order to obtain the key:value pairs of the second map more efficiently and save me time on having to continuously write "some word" instead just call from its index key But after looking at it I dont think its that useful I should instead use what you suggest Set<Pair<String, String>>
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
verbose
verboseOP3y ago
god if this was python it would be soo nice or any other language that can make this efficient Im okay with using an extra library if it does prove to be more effective I will look into your suggestion, I thank you for all the help!
Kyo-chan
Kyo-chan3y ago
This isn't about efficiency, it's just impossible to come and imagine that nested maps are a useful tool for that It would have been the same with Python
verbose
verboseOP3y ago
Ic well you are right its not really useful to use a nested map for something like what I am aiming for, but I was just testing some things out
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@400463761011507212>.
Want results from more Discord servers?
Add your server