I have a static HashMap <Integer, String> and an Arraylist.
public static final HashMap<Integer, String> CONSTANT_MAP =
(HashMap<Integer, String>) Collections.unmodifiableMap(new HashMap<Integer, String>() {
{
put(28, "Action");
put(12, "Adventure");
put(16, "Animation");
put(35, "Comedy");
put(80, "Crime");
put(99, "Documentary");
put(18, "Drama");
put(10751, "Family");
put(14, "Fantasy");
put(36, "History");
put(27, "Horror");
put(10402, "Music");
put(9648, "Mystery");
put(10749, "Romance");
put(878, "Science Fiction");
put(10770, "TV Movie");
put(53, "Thriller");
put(10752, "War");
put(37, "Western");
}
});
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. I tried something like that but it doesn't work... I get ExceptionInInitializerError.
public static ArrayList<String> getGender(ArrayList<Integer> arrayList, HashMap<Integer, String> hashMap) {
ArrayList<String> resultList = new ArrayList<String>();
for (Map.Entry m : hashMap.entrySet()) {
if (arrayList.contains(m.getValue()))
resultList = (ArrayList<String>) m.getValue();
}
return resultList;
}
CodePudding user response:
well, if you just want to check whether the keys in hashmap is equal to the arraylist, you could use hashMap.keySet().equals(new HashSet<Integer>(arrayList))
, and then use hashMap.values()
to get all the values.
CodePudding user response:
The answer from Alexander Ivanchenko correctly identifies the cause of the exception you're seeing (so you should accept his answer!). My answer talks below talks about other problems with your code.
Your hashmap contains values that are Strings:
HashMap<Integer, String> hashMap
therefore
m.getValue()
is a String, and therefore you cannot cast it to an ArrayList of Strings
resultList = (ArrayList<String>) m.getValue();
I think what you want is to accumulate values in your result list:
resultList.add(m.getValue());
Additionally, as pointed out by @racraman, this comparison is problematic:
if (arrayList.contains(m.getValue())
The list arrayList
contains Integers; m.getValue() is a String; I am surprised this compiles at all. I think from the description you meant
if (arrayList.contains(m.getKey())
so maybe was a transcription error when writing this post?
CodePudding user response:
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.
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 withArrayList
elements. If the keys are equal theHashmap
value should be put to a separateArrayList
and be returned.
Firstly, judging by the contents of the map I believe you've misspelled its name, and you meant genre
, not gender
. In the code below I changed it, as well as parameter-names and types.
The most efficient way to approach this task is to iterate over the list and check whether a particular key is contained in the map. Not the opposite like you've done in you're 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).
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;
}
Or
public static List<String> getGenre(List<Integer> arrayList, Map<Integer, String> hashMap) {
Map<Integer, String> result = new HashMap<>(genreById);
result.keySet().retainAll(new HashSet<>(ids));
return new ArrayList<>(result.values());
}