➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method 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!