In Java, converting an Object to a List is not a direct and feasible operation, because Object is the root class of all classes in Java and does not have the structure or behavior of the list itself. However, if your Object is actually a class or structure containing a list (such as an array, collection, or other type of container), you can create a new List by type conversion or extracting the data in it.
The following are several common situations and their treatment methods:
1. If Object is an array
You can use the() method to convert an array into a list:
Object obj = new Integer[]{1, 2, 3, 4}; // Assume this is an Integer arrayList<?> list = ((Object[]) obj);
Note that casting is required here(Object[])
,because()
Expect an array of concrete types.
2. If the Object is a collection (such as Collection)
You can directly convert type:
Object obj = (1, 2, 3, 4); // Assume this is a List<Integer>List<?> list = (List<?>) obj;
3. If Object is a custom object containing a list
Suppose you have a custom class with a list:
class MyObject { private List<Integer> numbers; // Constructor, getter and setterpublic MyObject(List<Integer> numbers) { = numbers; } public List<Integer> getNumbers() { return numbers; } } // useObject obj = new MyObject((1, 2, 3, 4)); List<Integer> list = ((MyObject) obj).getNumbers();
4. Use reflection (not recommended unless necessary)
If you don't know the specific type of Object, but are sure that it contains some form of list structure, you can use reflection to try to access these structures. However, this approach is very unrecommended because it is neither safe nor efficient and is prone to errors.
5. Convert to a general list (type Object)
If you just want to put the Object (regardless of its actual type) into a List, you can create a list that contains only one element:
Object obj = new Integer(123); List<Object> list = (obj);
Or, if you have an unsure typeObject
array or collection and want to put them all into oneList<Object>
In, you can do this:
Object[] array = new Object[]{1, "two", 3.0}; List<Object> list = (array); // This is still a fixed size list containing array elements// Or if you want a variable size list:List<Object> mutableList = new ArrayList<>((array));
Note that the list created here (array) is of a fixed size and contains the entire array as a single element. If you want each element in the array to be a separate element of the list, you need to iterate over the array and add one by one.
Summarize
Converting an Object to a List usually requires you to understand the actual type of the Object and perform appropriate type conversion or data extraction. In most cases, direct type conversion or using() is the easiest and most straightforward way.
This is the end of this article about five methods to convert Object into List by Java. For more related Java Object to List content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!