[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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • 17 September

    Create List with One Element in Java

    Table of ContentsUsing Collections.singletonList()Using Array.asList() method [ Immuatable list]Using new ArrayList with Array.asList() method [ Mutable list] In this post, we will see how to create List with One Element in java.. Using Collections.singletonList() This is best way to create List with single element if you need an immutable List. [crayon-632f6a74730f0507955668/] Output [crayon-632f6a74730f6786087260/] If you […]

  • 17 May

    How to Add Multiple Values for Single Key In HashMap in Java

    Table of ContentsHashMapCan HashMap Store Multiple Values AutomaticallyWays to Add Multiple Values for Single Key In HashMap in JavaUsing the Standard LibraryUsing Apache Commons LibraryUsing Google Guava LibraryUsing TreeSet as ValuesUsing a Wrapper ClassUsing Java TuplesUsing compute() Function in JDK 8Conclusion This article discusses the HashMap in Java and how to add multiple values for […]

  • 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 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.