#HashMap with Objects

9 messages · Page 1 of 1 (latest)

buoyant mirage
#

I have created a HashMap that follows the pairing : <String, Object> and it functions as a data holder for most of the String: Object pairs I intend to add into it.

Purpose
I am creating a program that allows user input on determining whether a certain word is the correct opposite (antonym which is inputted by the user). For example, I would request the user to input the opposite of absent ; The possibilities are as follows:

  1. Correct Absent : Present (or any related antonym) : Absent ✔️

or

  1. Incorrect Absent : Zebra : Absent ❌

My code for creating the HashMap of <String, Object> pairs is as follows:

public static HashMap<String, Object> commonOpposites = new HashMap<>() {{
        put("absent", List.of("present", "alert", "attentive"));        put("accurate", "inaccurate");
}};

Question
How would I access anything within the List reference? If I were to check if user input is in fact one of the elements within the List, would I use contains() within a if statement?

I appreciate all the help and support for anyone who can look into this!

rocky groveBOT
#

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

night token
#

don't use Object but List<String> as values

#

so Map<String, List<String>>

buoyant mirage
#

I would use that but the purpose of having <String, Object> is so that I wont need to add a list when working with only 1 opposite such as zip - unzip (there would be no purpose of having a list of just 1 element)

night token
#

just use a list with a single element

#

makes stuff easier

buoyant mirage
#

I guess you maybe right