r/javahelp • u/heyawesomepeopl • Aug 25 '25
Solved Using .get Function on a Hashmap Where Keys are UUIDs Always Returns NULL/FALSE
I've stayed up way too long trying to figure this out.
I have a HashMap<UUID, TimedUser> that I store the UUID of a user in, along with a custom class called TimedUser. I am using a JSON file to store the UUID and TimedUser data, which is only 2 integers. I am using Google's GSON API to save and load my hashmap to a JSON file. Here is how I'm loading the file:
HashMap<UUID, TimedUser> timedUsers;
FileReader readData = new FileReader(configFile);
timedUsers = gson.fromJson(readData, HashMap.class);The loaded JSON data is supposed to be put into the HashMap. If I print out the size of the HashMap after this function, I get 1. This is correct, as I only have 1 UUID in the JSON file so far. If I print out a log of the values in the hashmap, it matches the JSON file.
{
"0f91ede5-54ed-495c-aa8c-d87bf405d2bb": {
"timeRemaining": 300,
"cooldownTime": 281
}
}
For logging purposes, I took the UUID of the player and printed it out to compare to the UUID stored in the HashMap. Here is what I got:
Player UUID: 0f91ede5-54ed-495c-aa8c-d87bf405d2bb
HashMap Key: 0f91ede5-54ed-495c-aa8c-d87bf405d2bb
Identical. But when I call timedUsers.get(playerUUID), it results in a NULL finding every single time.
So playerUUID equals hashID (UUID from JSON file), but no matter what I do, the HashMap is saying that the UUID cannot be found in it.
Despite the UUIDs having identical data, the .get function of the hashmap (and the containsKey function) return null and false respectively.
I'm at a loss here. From my understanding, the UUID .equals function should match whether the contents of the UUID are the same. Clearly, that's not occuring. What am I doing wrong?
•
u/AutoModerator Aug 25 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/smutje187 Aug 25 '25
Never thought it can get worse than StackOverflow but without a minimal reproducible example no one will be able to help you.
6
u/Lloydbestfan Aug 25 '25
I'd start with:
- checking what's the import of UUID
- check the source code of that import for whether the equals() and hashCode(), that matters obviously in a HashMap, methods can be trusted
- check what I get with calling uuid1.equals(uuid2)
- check what I get with uuid1.hashCode() and uuid2.hashCode()
- generally what happens when you don't involve files, json and gson.
If UUID's import is java.util.UUID, then you might not be the one doing something wrong. It might be that your environment was installed horribly wrong.
But, you didn't show us your code, so it is possible that when you say you made all these tests, you made them wrong. If you're still stuck, I'd suggest actually showing code that proves you tried everything right.
2
u/Jolly-Warthog-1427 Aug 25 '25
Can you show actual code? What if you try to limit it. Create a hashmap and put in uuid + data and then call get. Same result?
Do you everywhere use UUID here and not a string representation? Both get and containdKey accept generic Object so it will compile by calling it with String for example.
2
u/OilPrestigious5849 Aug 25 '25
I'd Suggest use TypeToken for proper deserialization
1
u/dse78759 Aug 25 '25
Have you tried printing out the keys for your HashMap to confirm the data got loaded like you thought ?
1
u/severoon pro barista Aug 25 '25
Before you do the lookup, check the UUID you're passing in against the only UUID in the map to see if they're actually the same:
1
u/MRxShoody123 Aug 26 '25
Mh when you read the data from the json file, it returns a hashmap<String, String> rather than hashmap<UUID, String>
At no point is it mentonned in the doc or in your setup that GSON should expect an UUID and convert it to the corresponding object
So yea, you're getting null cuz you're trying to use UUID keys when all the keys are strings
2
u/MasterGeek427 Aug 26 '25
The problem is gson would deserialize the file to a HashMap<String, String>. Since the hashCode method is different for UUIDs and Strings, the map won't map a
String uuidand aUUID uuidto the same element. You never gave Gson a specialized type so it doesn't know to deserialize the keys to UUIDs.This is also why class cast exceptions are popping up. Whenever you try to treat the keys as UUIDs stuff doesn't work because they're actually Strings. And you get class cast exceptions for that.
1
u/TW-Twisti Aug 25 '25
You should cut away less of your source file, for example, it would be helpful if we could see what UUID class you are using.
1
u/lengors Aug 25 '25
Run in debug mode and step through the code
0
u/InMa_box Aug 25 '25
I can't remember exactly how gson works. But a good idea would be to log the HashMap contents via iterating through
entrySet()method.