All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hframework.common.util.collect.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.hframework.common.util.collect;


import com.hframework.common.util.collect.bean.*;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * User: zhangqh6
 * Date: 2015/11/17 17:03:03
 */
public class CollectionUtils {

    /**
     * 数据分组
     * @param list
     * @param 
     * @param 
     * @return
     */
    public static  Map> group(List list, Grouper grouper) {
        Map> retMap = new LinkedHashMap>();
        if(list != null && list.size() > 0) {
            for (V value : list) {
                K key = grouper.groupKey(value);
                if(!retMap.containsKey(key)) {
                    retMap.put(key, new ArrayList());
                }
                retMap.get(key).add(value);
            }
        }
        return retMap;
    }
    /**
     * List转Map
     * @param list
     * @param 
     * @param 
     * @return
     */
    public static  Map convert(List list, Mapper mapper) {
        Map retMap = new LinkedHashMap();
        if(list != null && list.size() > 0) {
            for (V value : list) {
                K key = mapper.getKey(value);
                retMap.put(key, value);
            }
        }
        return retMap;
    }

    /**
     * List转Map
     * @param list
     * @param merger
     * @param 
     * @param 
     * @return
     */
    public static  int search(List list,V data, Merger merger) {
        K dataKey = merger.getKey(data);
        if(list != null && list.size() > 0) {
            for (V value : list) {
                K key = merger.getKey(value);
                if(key.equals(dataKey)) {
                    return list.indexOf(value);
                }
            }
        }
        return -1;
    }

    /**
     * List转Map
     * @param map
     * @param 
     * @param 
     * @param merger
     * @return
     */
    public static  K search(Map> map, V data, Merger merger) {
        K dataKey = merger.getKey(data);
        for (Map.Entry> mapEntry : map.entrySet()) {
            int index = search(mapEntry.getValue(), data, merger);
            if(index > -1) {
                return mapEntry.getKey();
            }
        }
        return null;
    }

    /**
     * 列表更新
     * @param srcList 被更新的列表
     * @param destList 更新列表
     * @param mapper 合并器
     * @param  对象
     * @param  Value
     * @return
     */
    public static  List update(List srcList, List destList, Mapper mapper) {
        //更新列表为空,直接返回源对象即可
        if(destList == null || destList.isEmpty()) {
            return srcList;
        }

        //源对象为空,进行初始化
        if(srcList == null) {
            srcList = new ArrayList();
        }

        //循环更新列表,判断对应的值是否存在被更新列表中,
        // 如果存在进行替换,如果不存在直接添加一个新的对象
        flag : for (V dest : destList) {
            K dataKey = mapper.getKey(dest);
            for (V value : srcList) {
                K key = mapper.getKey(value);
                if(key.equals(dataKey)) {
                    srcList.set(srcList.indexOf(value),dest);
                    break flag;
                }
            }
            srcList.add(dest);

        }
        return srcList;
    }

    /**
     * Map更新
     * @param srcMap
     * @param destMap
     * @param mapper
     * @param 
     * @param 
     * @return
     */
    public static  Map> update(Map> srcMap, Map> destMap, Mapper mapper) {
        //更新列表为空,直接返回源对象即可
        if(destMap == null || destMap.isEmpty()) {
            return srcMap;
        }

        //源对象为空,进行初始化
        if(srcMap == null) {
            srcMap = new HashMap();
        }

        //循环Map对象进行值列表更新
        for (K key : destMap.keySet()) {
            List destList = destMap.get(key);
            List srcList = srcMap.get(key);
            srcList = update(srcList, destList, mapper);
            srcMap.put(key,srcList);

        }

        return srcMap;
    }

    /**
     * 列表删除
     * @param srcList 总列表
     * @param destList 删除列表
     * @param merger 合并器
     * @param  对象
     * @param  Value
     * @return
     */
    public static  List remove(List srcList, List destList, Merger merger) {
        //更新列表为空,直接返回源对象即可
        if(destList == null || destList.isEmpty()) {
            return srcList;
        }

        //源对象为空,进行初始化
        if(srcList == null) {
            return srcList;
        }

        //循环待删除列表,判断对应的值是否存在总列表中,
        // 如果存在直接删除,否则不做任何处理
        for (V dest : destList) {
            int index = CollectionUtils.search(srcList, dest, merger);
            if(index > -1) {
                srcList.remove(index);
            }
        }
        return srcList;
    }

    public static  List copy(List originList) {
        if(originList == null) {
            return null;
        }

        List targetList = new ArrayList();

        for (E e : originList) {
            try {
                targetList.add((E) BeanUtils.cloneBean(e));
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            }
        }

        return targetList;
    }

    /**
     * Map删除
     * @param srcMap
     * @param destMap
     * @param merger
     * @param 
     * @param 
     * @return
     */
    public static  Map> remove(Map> srcMap, Map> destMap, Merger merger) {
        //更新列表为空,直接返回源对象即可
        if(destMap == null || destMap.isEmpty()) {
            return srcMap;
        }

        //源对象为空,无需删除,直接返回
        if(srcMap == null) {
            return srcMap;
        }

        //循环Map对象进行列表值删除操作
        for (K key : destMap.keySet()) {
            List destList = destMap.get(key);
            List srcList = srcMap.get(key);
            srcList = remove(srcList, destList, merger);
            srcMap.put(key,srcList);
        }
        return srcMap;
    }


    public static  List fetch(List originList, Fetcher fetcher) {
        if(originList == null) {
            return null;
        }
        List targetList = new ArrayList();
        for (F f : originList) {
            targetList.add(fetcher.fetch(f));
        }
        return targetList;
    }

    public static  List from(List originList, Mapping mapping) {
        if(originList == null) {
            return null;
        }
        List targetList = new ArrayList();
        for (F f : originList) {
            targetList.add(mapping.from(f));
        }
        return targetList;
    }


    public static void main(String[] args) {
        List userList = new ArrayList();
        userList.add(new UsrBean(1,"张三",29));
        userList.add(new UsrBean(2,"李四",28));
        userList.add(new UsrBean(3,"王二",27));
        userList.add(new UsrBean(4,"李四",29));

        Map> merge = CollectionUtils.group(userList, new Grouper() {
            public Integer groupKey(UsrBean usrBean) {
                return usrBean.getAge();
            }
        });

        Map convert = CollectionUtils.convert(userList, new Mapper() {
            public Integer getKey(UsrBean usrBean) {
                return usrBean.getAge();
            }

            public Integer groupKey(UsrBean usrBean) {
                return usrBean.getAge();
            }
        });

        System.out.println(1111);
    }


    /**
     *
     * @param map
     * @param key
     * @param aValue
     */
    public static   void  addMapValue(Map> map, K key, V aValue) {
            if(!map.containsKey(key)) {
                map.put(key,new ArrayList());
            }
            map.get(key).add(aValue);
    }

    /**
     *
     * @param map
     * @param key
     * @return
     */
    public static    List getMapValue(Map> map, K key) {
        if(!map.containsKey(key)) {
            map.put(key,new ArrayList());
        }
        return map.get(key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy