Cast Object to List String in Java

Cast Object to List String in Java | The Object class is the super class of all Java classes. Therefore it can hold all the types of objects in Java. Sometimes objects may hold a list of strings and to get those list of strings we need to cast the object.

The below program demonstrate how to cast the object to a list string in Java.

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

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("CSS");
        Object object = list;

        // cast object to list string
        List<String> listString = (List<String>) object;
        System.out.println(listString);
        System.out.println(listString.getClass().getName());
    }
}

Output:-

[Java, Python, CSS]
java.util.ArrayList

Note that if an object holds some other type of value instead of the list of strings then we can’t cast the object to list string. It will give java.lang.ClassCastException saying class java.lang.Object cannot be cast to class java.util.List.

Let us see an example for this where casting object to list string leads to java.lang.ClassCastException.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        Object object = new Object();

        // cast object to list string
        List<String> listString = (List<String>) object;
        System.out.println(listString);
        System.out.println(listString.getClass().getName());
    }
}

It gives the following exception:-

Exception in thread “main” java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.util.List (java.lang.Object and java.util.List are in module java.base of loader ‘bootstrap’) at Main.main(Main.java:8)

In the below program object holds a set of strings, but not the list of strings. Therefore, the below program gives java.lang.ClassCastException exception.

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");
        set.add("CSS");
        Object object = set;

        // cast object to list string
        List<String> listString = (List<String>) object;
        System.out.println(listString);
        System.out.println(listString.getClass().getName());
    }
}

It gives the following exception:-

Exception in thread “main” java.lang.ClassCastException: class java.util.HashSet cannot be cast to class java.util.List (java.util.HashSet and java.util.List are in module java.base of loader ‘bootstrap’) at Main.main(Main.java:14)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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