java List 속성 별로 도구 클래스 나 누 기

3251 단어 자바 도구
package com.iflytek.bim.dms.common.core.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xghuang
 * @date 2018/6/22
 * @time 13:23
 * @desc:
 */
public class GroupUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(GroupUtil.class);

    /**
     *  List  V methodName     (      K  )  ,   Map> 
* method V , K * * @param list * @param map map * @param clazz V * @param methodName */ public static void listGroup2Map(List list, Map> map, Class clazz, String methodName) { // if (null == list || null == map || null == clazz || StringUtils.isBlank(methodName)) { LOGGER.error("CommonUtils.listGroup2Map ,list:" + list + " ;map:" + map + " ;clazz:" + clazz + " ;methodName:" + methodName); return; } // Method method = getMethodByName(clazz, methodName); // if (null == method) { return; } // listGroup2Map(list, map, method); } /** * List V ( K ) , Map>
* method V , K * * @param list * @param map map * @param method */ @SuppressWarnings("unchecked") private static void listGroup2Map(List list, Map> map, Method method) { // if (null == list || null == map || null == method) { LOGGER.error("CommonUtils.listGroup2Map ,list:" + list + " ;map:" + map + " ;method:" + method); return; } try { // Object key; List listTmp; for (V val : list) { key = method.invoke(val); listTmp = map.get(key); if (null == listTmp) { listTmp = new ArrayList(); map.put((K) key, listTmp); } listTmp.add(val); } } catch (Exception e) { LOGGER.error(" !", e); } } /** * , * * @param clazz * @param methodName * @return */ private static Method getMethodByName(Class> clazz, String methodName) { Method method = null; // if (null == clazz || StringUtils.isBlank(methodName)) { LOGGER.error("CommonUtils.getMethodByName ,clazz:" + clazz + " ;methodName:" + methodName); return method; } try { method = clazz.getDeclaredMethod(methodName); } catch (Exception e) { LOGGER.error(" !", e); } return method; } }

좋은 웹페이지 즐겨찾기