HashMap with Objects

I have created a HashMap that follows the pairing :
<String, Object>
<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 ✔️
Correct Absent : Present (or any related antonym) : Absent ✔️
or 2.
Incorrect Absent : Zebra : Absent ❌
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");
}};
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!
6 Replies
JavaBot
JavaBot3y ago
Hey, @verbose! Please remember to /close this post once your question has been answered!
dan1st
dan1st3y ago
don't use Object but List<String> as values so Map<String, List<String>>
verbose
verboseOP3y ago
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)
dan1st
dan1st3y ago
just use a list with a single element makes stuff easier
verbose
verboseOP3y ago
I guess you maybe right
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@400463761011507212>.
Want results from more Discord servers?
Add your server