Object转换List

简介: Object转换List「Map「String,Object」」

使用场景
使用的时候IDEA会提示警告 说未检查类型.
解决办法

使用@SuperWarning({“unchecked”})进行压制
写个工具类进行转换
AI 代码解读

写的时候参考了我的 Object 转换 List的写法,只是说在处理o的时候再次进行了转换获得每个key和value.
写完Object转换List后大概想了1个多小时才想到这个方式, 真的佩服自己的愚蠢
public static List> objConvertListMap(Object obj) throws IllegalAccessException {
List> result = new ArrayList<>();
if (obj instanceof List<?>){
for (Object o : (List<?>) obj) {
Map map = new HashMap<>(16);
Class<?> clazz = o.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String key = field.getName();
Object value = field.get(key);
if (value == null){
value = "";
}
map.put(key,value);
}
result.add(map);
}
return result;
}
return null;
}

public static <V> List<Map<String,V>> objConvertListMap(Object obj, Class<V> vClass) throws IllegalAccessException {
    List<Map<String, V>> result = new ArrayList<>();
    if (obj instanceof List<?>) {
        for (Object o : (List<?>) obj) {
            Map<String, V> map = new HashMap<>(16);
            Class<?> oClass = o.getClass();
            for (Field field : oClass.getDeclaredFields()) {
                field.setAccessible(true);
                String key = field.getName();
                Object value = field.get(key);
                if (value == null) {
                    value = "";
                }
                map.put(key, vClass.cast(value));
            }
            result.add(map);
        }
        return result;
    }
    return null;
}
AI 代码解读

这样就不会局限在转换到List>这一种类型上了.
可以转换成List>上等,进行泛型转换
虽然多了一个参数,但是可以重载啊
感觉field.get(key) 这里处理的不是很好,如果有更好的办法可以留言
public static List> castListMap(Object obj, Class kCalzz, Class vCalzz) {
List> result = new ArrayList<>();
if (obj instanceof List<?>) {
for (Object mapObj : (List<?>) obj) {
if (mapObj instanceof Map<?, ?>) {
Map map = new HashMap<>(16);
for (Map.Entry<?, ?> entry : ((Map<?, ?>) mapObj).entrySet()) {
map.put(kCalzz.cast(entry.getKey()), vCalzz.cast(entry.getValue()));
}
result.add(map);
}
}
return result;
}
return null;
}

目录
打赏
0
0
0
0
14
分享
相关文章
|
3月前
|
Python错误 - 'list' object is not callable 的问题定位与解决
出现编程问题并不可怕,关键在于是否可以从中学习与成长。遇到'list' object is not callable这样的错误,我们不仅需要学会应对,更需要了解其背后的原因,避免类似的问题再次出现。记住,Python的强大功能和灵活性同时也意味着我们需要对其理解更准确,才能更好的使用它。
384 70
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
290 0
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
Redis第四弹,Redis实现list时候做出的优化ziplist(压缩链表,元素少的情况),可更好的节省空间list——(内部编码:quicklist)Object encoding
Redis第四弹,Redis实现list时候做出的优化ziplist(压缩链表,元素少的情况),可更好的节省空间list——(内部编码:quicklist)Object encoding
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
287 1
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
471 0
解决Java中的“Unchecked cast: java.lang.Object to java.util.List”问题
解决Java中的“Unchecked cast: java.lang.Object to java.util.List”问题
1013 0
List<Map<String, Object>>,Map<String,List<Map<String, Object>>>多方式循环遍历
List<Map<String, Object>>,Map<String,List<Map<String, Object>>>多方式循环遍历
253 0
AttributeError: ‘list‘ object has no attribute ‘ndim‘
AttributeError: ‘list‘ object has no attribute ‘ndim‘
316 0

热门文章

最新文章