Object转list
时间: 2023-10-10 14:15:14 浏览: 133
要将Object对象转换为List对象,可以使用以下方法:
```
public static <T> List<T> castList(Object obj, Class<T> clazz) {
if (obj instanceof List) {
return (List<T>) obj;
} else {
return Collections.singletonList(clazz.cast(obj));
}
}
```
这个方法接受两个参数,一个是obj,即是我们需要转换的Object对象,另一个是List中元素的class类型。首先,我们判断obj是否是List类型,如果是的话直接返回转换后的List对象。如果不是List类型,则使用Class.cast方法将obj转换为clazz类型,并使用Collections.singletonList方法将其封装为单个元素的List对象。通过这个方法,就可以将Object对象成功转换为List对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [如何优雅的将Object转换成List](https://blog.csdn.net/VioletHan7/article/details/103715005)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Object型转list,jsonObject型转list方法,亲测可用](https://blog.csdn.net/httpmc2018/article/details/122897742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文