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

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList.

ClassCastException is runtime exception which indicate that code has tried to cast an object to a subclass which is not an instance.

Reason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Although static method Arrays.asList() returns List, it returns java.util.Arrays$ArrayList which is different from ArrayList, so when we try to cast Arrays.asList() to ArrayList, it throws exception.

Let’s understand with the help of example:

Output:

Here is source code of Arrays.asList() method.

As you can see that there is static inner class present in java.util.Arrays which is different from java.util.ArrayList.

Fixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Use ArrayList’s constructor

If you must have instance of java.util.ArrayList, then you can create ArrayList instance using ArrayList‘s constructor.

Here is complete program

Output:

[India, China, Bhutan]

Here is source code of ArrayList’s constructor

Assign Arrays.asList() to List reference rather than ArrayList

We can declare List rather than ArrayList to avoid the exception. Since java.util.Arrays$ArrayList implements List interface, we can assign it to List reference.

Here is complete program

Output:

[India, China, Bhutan]

That’s all about how to fix [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList.


import_contacts

You may also like:

Related Posts

  • Java ArrayList of objects
    25 January

    Create ArrayList of Objects in Java

    Table of ContentsUsing the add() method to create ArrayList of objects in javaUsing parameterized constructor to create ArrayList of objects in javaUsing addAll() method to create ArrayList of objects in javaConclusion In this tutorial, we will learn how to create ArrayList of objects in Java. We will create a Book class with different properties and […]

  • 10 September

    How to remove element from Arraylist in java while iterating

    Table of ContentsIntroductionUsing Collection’s removeIf() methodUsing ListIterator classUsing removeAll() methodUsing Java 8 Stream to filter List itemsConclusion Introduction In this tutorial, you will learn how to remove element from Arraylist in java while iterating using different implementations provided by Java. It is necessary to understand the right way to remove items from a List because […]

  • Print HashMap in java
    30 June

    Print HashMap in Java

    Table of ContentsPrint from the HashMap ReferencePrint HashMap using foreach method with keyset()Print HashMap using Consumer with entrySet()Print HashMap using Arrays’s asList() methodPrint HashMap using Collections’s singletonList()Print HashMap using getkey() and getValue with entrySet()Print HashMap using BiConsumerPrint HashMap using IteratorPrint HashMap using custom ObjectsConclusion In this article, we will see how to print HashMap in […]

  • Print LinkedList in java
    26 June

    Print LinkedList in java

    Table of ContentsJava inbuilt LinkedListPrint LinkedList using a for loopPrint LinkedList using Enhanced for loopPrint LinkedList using streamsPrint LinkedList using toString() methodPrint LinkedList using IteratorPrint LinkedList using custom objectsCustom singly LinkedListHow to print a custom singly LinkedListConclusion Java inbuilt LinkedList Java inbuilt LinkedList class uses doubly linked list as internal data structure to store its […]

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Print ArrayList in java
    11 May

    Print ArrayList in Java

    Learn about how to print ArrayList in java in different ways.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.