zoukankan      html  css  js  c++  java
  • list转map

    参考https://cloud.tencent.com/developer/article/1033706

    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class ListToMap {
        //list转化为map
        public static <K, V> Map<K, V> listToMap(List<V> vList, String kName) {
            Map<K, V> map = new HashMap<K, V>();
            if (vList == null || kName == null || vList.size() == 0) {
                return map;
            }
            Field kField = getField(vList.get(0).getClass(), kName);
            for (V v : vList) {
                try {
                    map.put((K) kField.get(v), v);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return map;
        }
        //获取key对应的类型字段
        private static <V> Field getField(Class<V> clazz, String kName) {
            if (clazz == null || kName == null) {
                return null;
            }
            for (Field fieldElem : clazz.getDeclaredFields()) {
                fieldElem.setAccessible(true);
                if (fieldElem.getName().equals(kName)) {
                    return fieldElem;
                }
            }
            return null;
        }
        //调用示例
        public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
            List<Person> pList = new ArrayList<Person>();
            Person p1 = new Person(2, "taxuexing");
            pList.add(p1);
            Person p2 = new Person(3, "jikewang");
            pList.add(p2);
            Map<Integer, Person> map = ListToMap.listToMap(pList, "age");
    
            System.out.println(map.get(2).getUserName());
            System.out.println(map.get(3).getUserName());
        }
    }
    

      

    public class Person {
        private String userName;
        private int age;
        Person(){
    
        }
        Person(int age, String userName){
            this.age = age;
            this.userName = userName;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

      

  • 相关阅读:
    Hadoop HDFS的Shell操作实例
    我来阿里的2年
    设计模式之迪米特原则(LoD)
    设计模式之接口隔离原则
    设计模式之依赖倒置原则
    设计模式之里氏替换原则(LSP)
    设计模式之单一职责原则(SRP)
    Android开发系列之性能优化
    Android开发系列之屏幕密度和单位转换
    Android开发系列之ListView
  • 原文地址:https://www.cnblogs.com/alice-cj/p/10357685.html
Copyright © 2011-2022 走看看