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

HashMap values cannot be cast to list

In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List.

Why HashMap values cannot be cast to list?

HashMap values returns java.util.Collection and you can not cast Collection to List or ArrayList. It will throw ClassCastException in this scenario.

Let’s understand with the help of example:

package org.arpit.java2blog;

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

public class HashMapValuesToList {

    public static void main(String[] args) {
        Map nameAgeMap = new HashMap<>();
        nameAgeMap.put("John",23);
        nameAgeMap.put("Martin",20);
        nameAgeMap.put("Sam",28);

        List ageList = (List)nameAgeMap.values();
        System.out.println(ageList);
    }
}

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 org.arpit.java2blog.HashMapValuesToList.main(HashMapValuesToList.java:15)

Here is source code of HashMap’s values method.

public Collection values() {
    Collection vs = values;
    return (vs != null ? vs : (values = new Values()));
}

Fix for java.util.HashMap$Values cannot be cast to class java.util.List

We can use ArrayList‘s constructor which takes Collection as parameter to resolve this issue.

List ageList =  new ArrayList<>(nameAgeMap.values());

Here is complete program

package org.arpit.java2blog;

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

public class HashMapValuesToList {

    public static void main(String[] args) {
        Map nameAgeMap = new HashMap<>();
        nameAgeMap.put("John",23);
        nameAgeMap.put("Martin",20);
        nameAgeMap.put("Sam",28);

        // Use ArrayList's constructor
        List ageList = new ArrayList<>(nameAgeMap.values());
        System.out.println(ageList);
    }
}

Output:

[23, 20, 28]

Here is source code of ArrayList’s constructor

 /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

That’s all about how to fix java.util.HashMap$Values cannot be cast to class java.util.List.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.