#Obtain Value from nested Hashtable Map

23 messages · Page 1 of 1 (latest)

cobalt cradle
#

I am creating a program that initially uses nested Hashtables in the format:

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} 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"));
    ...
}};

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" );

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!

tidal phoenixBOT
#

Hey, @cobalt cradle!
Please remember to /close this post once your question has been answered!

fallow gyro
#

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");

cobalt cradle
#

Ic Ic it does seem very straightforward but I have had some difficulty using it within an if statement

#

basically ```java
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 ```java
What is the opposite of absent?
User In: present
Correct!

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

quiet dragon
#

What is the use of the first map? Int index is not used as far as i see. Would it be an option to switch to a set of maps or even switch to pair from apache commons lang3? Set<Pair<String,String>>? Since you call entryset on a map anyway?

#

set.stream().map(pair -> pair.getLeft().equals(someInput)).map(Pair::getRight).findFirst().ifPresentOrElse(v -> logger.info("found " + v), () -> logger.info("not found"))

#

Or use Map<String,String>?

#

But then your key is unique

fallow gyro
#

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

cobalt cradle
#

But after looking at it I dont think its that useful I should instead use what you suggest Set<Pair<String, String>>

quiet dragon
#

You can still use Set of maps if you dont want an extra library

cobalt cradle
cobalt cradle
#

I will look into your suggestion, I thank you for all the help!

fallow gyro
#

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

cobalt cradle
#

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