List - Map 工具类,list转为map
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                List - Map 工具类,list转为map
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            提供了List轉為Map的2種方法,第一種convertOne是常規(guī)轉換,以key作為map的key,以list中的E作為value;第二種則以key作為map的key,以list作為value
@SuppressWarnings("unchecked") final public class MapConverter {private static final String GET = "get";private MapConverter() {throw new AssertionError("Util禁止反射實例化");}public static <K, E> Map<K, E> convertOne(List<E> list, String key) {if (CollectionUtils.isEmpty(list)) {return null;}Map<K, E> map = null;try {Method getM = getMethod(list.get(0).getClass(), key);map = new HashMap<>();for (E en : list) {K k = (K) getM.invoke(en);map.put(k, en);}} catch (Exception e) {e.printStackTrace();}return map;}public static <K, E> Map<K, List<E>> convertList(List<E> list, String key) {if (CollectionUtils.isEmpty(list)) {return null;}Map<K, List<E>> map = null;try {Method getM = getMethod(list.get(0).getClass(), key);map = new HashMap<>();for (E en : list) {K k = (K) getM.invoke(en);List<E> res = map.get(k);if (res != null) {res.add(en);} else {List<E> l1 = new ArrayList<>();l1.add(en);map.put(k, l1);}}} catch (Exception e) {e.printStackTrace();}return map;}private static Method getMethod(Class clazz, String key) throws NoSuchMethodException {if (key.startsWith(GET)) {return clazz.getMethod(key);}if (Character.isUpperCase(key.charAt(0))) {clazz.getMethod(GET + key);}return clazz.getMethod(GET + Character.toUpperCase(key.charAt(0)) + key.substring(1));} }
                        
                        
                        @SuppressWarnings("unchecked") final public class MapConverter {private static final String GET = "get";private MapConverter() {throw new AssertionError("Util禁止反射實例化");}public static <K, E> Map<K, E> convertOne(List<E> list, String key) {if (CollectionUtils.isEmpty(list)) {return null;}Map<K, E> map = null;try {Method getM = getMethod(list.get(0).getClass(), key);map = new HashMap<>();for (E en : list) {K k = (K) getM.invoke(en);map.put(k, en);}} catch (Exception e) {e.printStackTrace();}return map;}public static <K, E> Map<K, List<E>> convertList(List<E> list, String key) {if (CollectionUtils.isEmpty(list)) {return null;}Map<K, List<E>> map = null;try {Method getM = getMethod(list.get(0).getClass(), key);map = new HashMap<>();for (E en : list) {K k = (K) getM.invoke(en);List<E> res = map.get(k);if (res != null) {res.add(en);} else {List<E> l1 = new ArrayList<>();l1.add(en);map.put(k, l1);}}} catch (Exception e) {e.printStackTrace();}return map;}private static Method getMethod(Class clazz, String key) throws NoSuchMethodException {if (key.startsWith(GET)) {return clazz.getMethod(key);}if (Character.isUpperCase(key.charAt(0))) {clazz.getMethod(GET + key);}return clazz.getMethod(GET + Character.toUpperCase(key.charAt(0)) + key.substring(1));} }
?
轉載于:https://www.cnblogs.com/zad27/p/10991138.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的List - Map 工具类,list转为map的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Java整合Spring发送邮件
- 下一篇: Django Rest框架 APIVie
