package com.heyu.api.common; import com.alibaba.fastjson.JSON; import com.heyu.api.common.test.AriseUser; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.ClassUtils; import java.lang.invoke.SerializedLambda; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j public class SanUtils { public static Map list2Map(List values, LBiFunction0 function) { try { if (CollectionUtils.isEmpty(values)) { return new HashMap<>(); } Map map = new HashMap<>(); Method method = getMethod(function); for (V value : values) { map.put((R) method.invoke(value), value); } return map; } catch (Exception e) { e.printStackTrace(); } return new HashMap<>(); } public static List list2listFilterNull(List values, LBiFunction0 function) { List list = list2list(values, function); List result = new ArrayList<>(); if(CollectionUtils.isEmpty(list)){ for (R r : list) { if(r!=null){ result.add(r); } } } return result; } public static List list2list(List values, LBiFunction0 function) { List list = new ArrayList<>(); try { if (CollectionUtils.isEmpty(values)) { return list; } Map map = new HashMap<>(); Method method = getMethod(function); for (V value : values) { list.add((R) method.invoke(value)); } return list; } catch (Exception e) { e.printStackTrace(); } return list; } public static Map> list2GroupBy(List values, LBiFunction0 function) { Map> listmap = new HashMap<>(); Method method = getMethod(function); try { for (int i = 0; i < values.size(); i++) { V v = values.get(i); R r = (R) method.invoke(v); List list = listmap.get(r); if (list == null) { list = new ArrayList<>(); } list.add(v); listmap.put(r, list); } } catch (Exception e) { e.printStackTrace(); } return listmap; } private static Method getMethod(LBiFunction0 function) { try { Method method = function.getClass().getDeclaredMethods()[1]; method.setAccessible(true); SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function); String methodName = serializedLambda.getImplMethodName(); String className = serializedLambda.getImplClass().replace("/", "."); Class clazz = Class.forName(className); Method methods[] = clazz.getDeclaredMethods(); for (Method m : methods) { if (m.getName().equals(methodName)) { return m; } } } catch (Exception e) { e.printStackTrace(); } return null; } private static List getMethods(LBiFunction0 function) { try { Method method = function.getClass().getDeclaredMethods()[1]; method.setAccessible(true); SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function); String methodName = serializedLambda.getImplMethodName(); String className = serializedLambda.getImplClass().replace("/", "."); Class clazz = Class.forName(className); List list = new ArrayList<>(); Method methods[] = clazz.getDeclaredMethods(); for (Method m : methods) { if (m.getName().equals(methodName)) { list.add(m); } } return list; } catch (Exception e) { e.printStackTrace(); } return null; } private static Method getRealMethod(List methods, Class paramClazz) { for (Method method : methods) { Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes != null && parameterTypes.length == 1 && ClassUtils.isAssignable(paramClazz, parameterTypes[0])) { return method; } } return null; } public static List convert(List list, LBiFunction0 function) { List methods = getMethods(function); List result = new ArrayList<>(); for (V v : list) { try { Class clazz = v.getClass(); Method method = getRealMethod(methods, clazz); int modifiers = method.getModifiers(); Object object = null; if (Modifier.isStatic(modifiers)) { object = method.invoke(null, v); } else { object = method.invoke(v); } result.add(object); } catch (Exception e) { log.error("convert exception", e); } } return result; } public static void main(String[] args) { AriseUser ai = new AriseUser(); ai.setName("张三"); ai.setAge(13); AriseUser ai2 = new AriseUser(); ai2.setName("李四"); ai2.setAge(23); AriseUser ai3 = new AriseUser(); ai3.setName("李四"); ai3.setAge(25); List list = new ArrayList<>(); list.add(ai); list.add(ai2); list.add(ai3); Map> map = list2GroupBy(list, AriseUser::getName); System.out.println(JSON.toJSONString(map)); } }