java 中 object 转 list
package com.taobao.txc.dubbo.get;
import java.util.ArrayList;
import java.util.List;
public class ceshi {
public static Object getObj() {
List<String> list = new ArrayList<>();
list.add("1");
list.add("ab");
return list;
}
public static <T> List<T> oobjectToList(Object obj, Class<T> clazz) {
List<T> result = new ArrayList<T>();
if (obj instanceof List<?>) {
for (Object o : (List<?>) obj) {
result.add(clazz.cast(o));
}
return result;
}
return null;
}
public static void main(String[] args) {
Object obj = getObj();
List<String> list = oobjectToList(obj, String.class);
list.forEach(System.out::println);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44