I get ExceptionInInitializerError
This error is triggered by an exception that occur during initialization of a static
field or while executing a static initializer block. A quote from the Javadoc:
An ExceptionInInitializerError
is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
In this case, you are trying to typecast a map returned by unmodifiableMap()
to HashMap
and this casting fails. It can't succeed because unmodifiableMap()
is meant to wrap any implementation of the Map
interface (TreeMap
, LinkedHashMap
, etc.). And this method returns an instance of class UnmodifiableMap
which implements Map
interface (i.e. it overrides all required methods) and maintains a variable of type Map
(a wrapped map that was passed as an argument) and delegates to calls to it. Inherently UnmodifiableMap
and HashMap
are siblings, they both implement Map
, but they don't extend each other, therefore you can't cast between these types.
A remedy: write your code against interfaces, not concrete types, and you'll be fine. CONSTANT_MAP
should be of type Map
, not HashMap
(by the way, I suggest to use more meaningful name like ganreById
). Similarly, an argument of your method should be a List
, not ArrayList
.
I need a method that would compare HashMap
keys with ArrayList
elements. If the keys are equal the Hashmap
value should be put to a separate ArrayList
and be returned.
Firstly, judging by the contents of the map I believe you've misspelled the method name, and you meant genre
, not gender
. In the code below I've changed it, as well as parameter-names (sure, names are totally up to you, it's only an example, but keep in mind that they have a huge impact on the readability of the code and hence important) and parameter types are changed to be the types of interfaces List
and Map
.
The most efficient way to approach this task is to iterate over the list and check whether a particular element is contained in the map.
Not the opposite like you've done in your code because contains()
check is costful on a list (you need to walk through all the elements to find out if the matching element exists), it's O(n) time if you're familiar with Big O notation. Conversely, containsKey()
check on a HashMap
is almost instant - O(1).
If the matching key exist, the value associated with it will get added to the resulting list.
public static List<String> getGenre(List<Integer> ids,
Map<Integer, String> genreById) {
List<String> result = new ArrayList<>();
for (Integer id: ids) {
if (genreById.containsKey(id))
result.add(genreById.get(id));
}
return result;
}
Sidenotes:
- If this method resides in the same class where the map is defined as a static field, passing it as parameter is redundant.
- You can substitute double-curly braces initialization (which isn't considered to be a good practice) with the static method
Map.ofEntries()
accessible with Java 9. It returns a map which is already unmodifiable, so there will be no need to wrap it with Collections.unmodifiableMap()
.