I have a static HashMap<UUID, MyObject> ALL = new HashMap<>(); which is used in multi-threading. To reproduce the error, I made this code: But, after some seconds (around 10), I get this error with Java 16 and Java 17: With Java 8, I get this: To test, I remove synchronized key-word, then try agai…
Tag: hashmap
How can I check if a hashmap contains a key of a custom class type?
I have a hashmap whose keys are of a custom class type (class A) and I want to check whether an child class B (B extends A) appears as a key in the map. IntelliJ gives me no warnings for the following code, but it’s not going into the statement. What could be wrong? Answer You are checking if the
Remove an Object from an arraylist in a hashmap
I need your help, I created this two classes I need a method in Graph to delete an Arch from the ArrayList that I have in the hashmap. I tried like this in the class Graph: but when I tested it in my test main the result is that nothing changed in my ArrayList in the hashmap. someone can help
What is the purpose of placing a HashSet inside of a HashMap?
For example: private HashMap<Integer, HashSet> variableName; I understand that HashMap implements Map and doesn’t allow duplicate keys and HashSet implements Set and doesn’t allow for duplicate values, but what is the purpose of placing a HashSet inside of a HashMap? Do they not achieve simi…
HashMap element is removed when another HashMap name remove an element
appearList is a HashMap with fixed data. I do: Then, when I remove an element from randomPersonList, that element on appearList is also removed unexpectedly. I expect to keep appearList as original. Can anyone tell me why and how to correct my code? Answer makes randomPersonList the reference to the same obje…
Table-like data structures that can handle different data types? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 10 months ago. Improve this question Are there any data structures and/or libraries in Java that will allow f…
Adding in hashmap by while loop but not working
I have the following CSV file and I would like to create a hashmap which the unique key should be the first value of each row and value should be an arraylist containing the information of each line under the first value of this row. Example CSV as below: So ideally, I would like the final ArrayList to be som…
Unboxing may produce Null Pointer Exception after checking if key exists in Map
Android Studio gives the warning: Unboxing of ‘idCollisonMap.get(currentId)’ may produce ‘NullPointerException’ even though I am checking if the key exists before I perform the Map.get(). Am I actually in danger of running into a null pointer exception? My understanding is that .contai…
How to Get data from nested LinkedHashMap
How to get the “test” data from this linkedHashMap response.get(“url”) = null response.get(“data.images.original.url”) = null response.get(“data”).get(“images”).get(“original”).get(“url”) toString(); result{data={images={origi…
How to sort the hashmap in descending order by values and if the values are the same then by key in ascending order
I have a HashMap<Integer, Integer> named “relevance” e.g {2: 3, 1: 3, 3: 3, 5: 4, 4: 4, 6: 3} and I want do DESC sorting by values. I get a hashmap {5: 4, 4: 4, 2: 3, 1: 3, 3: 3, 6: 3}. How can I sort ascending keys with the same values? Answer You can “chain” Comparators by