[Solved] java.util.HashMap$Values cannot be cast to class java.util.List

In this post, I will be sharing how to fix java.util.HashMap$Values cannot be cast to class java.util.List in Java. HashMap values cannot be cast to List because HashMap values return java.util.Collection and you cannot cast Collection to List or ArrayList. If you try to do so then you will get ClassCastException. We are sharing two solutions to fix this error.

Read Also: [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

[Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

As always, first, we will produce the java.util.HashMap$Values cannot be cast to class java.util.List error in Java before moving on to the solution.

1. Producing the error by trying to cast HashMap values to List


We can easily produce this error by trying to cast the HashMap values() method to List as shown below in the example.

import java.util.HashMap;
import java.util.List;

public class HashMapValuesNotCastToListExample {
    public static void main(String args[]) {
    
    // Creating HashMap object    
    HashMap<String,String> countryCurrencyMap = new HashMap<>();
    
    // Adding values to the HashMap object
    countryCurrencyMap.put("United States", "USD");
    countryCurrencyMap.put("India", "INR");
    countryCurrencyMap.put("Germany", "EUR");
    
List<String> list = (List<String>) countryCurrencyMap.values();
System.out.println(list); } }


Output:
Exception in thread "main" java.lang.ClassCastException: class java.util.HashMap$Values cannot be cast to class java.util.List (java.util.HashMap$Values and java.util.List are in module java.base of loader 'bootstrap')
    at HashMapValuesNotCastToListExample.main(HashMapValuesNotCastToListExample.java:13)


Explanation


According to Oracle docs, the HashMap values() method returns Collection and you cannot cast Collection to List or ArrayList. As a result, you get ClassCastException.

Solution1: Using Collection Reference


We can easily get rid of this error, by changing the reference from List to Collection as shown below in the example:

import java.util.HashMap;
import java.util.List;
import java.util.Collection;

public class HashMapValuesNotCastToListExample {
    public static void main(String args[]) {
    
    // Create HashMap object    
    HashMap<String,String> countryCurrencyMap = new HashMap<>();
    
    // Putting values to the HashMap object
    countryCurrencyMap.put("United States", "USD");
    countryCurrencyMap.put("India", "INR");
    countryCurrencyMap.put("Germany", "EUR");
    
Collection<String> list = countryCurrencyMap.values();
System.out.println(list); } }


Output:
[USD, EUR, INR]


Solution2: Using ArrayList's constructor


We can easily get rid of this error, by using the ArrayList's constructor which takes Collection as a  parameter as shown below in the example:

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class HashMapValuesNotCastToListExample {
    public static void main(String args[]) {
    
    // Making HashMap object    
    HashMap<String,String> countryCurrencyMap = new HashMap<>();
    
    // Add values to the HashMap object
    countryCurrencyMap.put("United States", "USD");
    countryCurrencyMap.put("India", "INR");
    countryCurrencyMap.put("Germany", "EUR");
    
List<String> list = new ArrayList<>(countryCurrencyMap.values());
System.out.println(list); } }


Output:
[USD, EUR, INR]


That's all for today. Please mention in the comments if you have any questions related to how to fix java.util.HashMap$Values cannot be cast to class java.util.List in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry