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

top.jfunc.common.utils.ArrayListMultimap Maven / Gradle / Ivy

package top.jfunc.common.utils;

import java.util.*;

/**
 * 自己实现的ArrayListMultimap 重复key的map.
 * 目前使用的地方 :
 * 1.使用监听的type,取出所有的监听器
 * 2.Http请求封装参数和header
 * @author L.cm email: [email protected] site:http://www.dreamlu.net date 2015年6月25日下午8:36:17
 */
public class ArrayListMultimap {

    private transient final Map> map;

    public ArrayListMultimap(){
        map = new HashMap<>();
    }
    public ArrayListMultimap(int capacity){
        map = new HashMap<>(capacity);
    }

    private static  List createList(){
        return new ArrayList<>();
    }

    /**
     * put to ArrayListMultimap
     * @param key 键
     * @param value 值
     * @return boolean
     */
    public boolean put(K key, V value){
        List list = map.get(key);
        if(list == null){
            list = createList();
            if(list.add(value)){
                map.put(key, list);
                return true;
            } else{
                throw new AssertionError("New list violated the list spec");
            }
        } else if(list.add(value)){
            return true;
        } else{
            return false;
        }
    }

    public Map> getMap() {
        return map;
    }

    /**
     * get List by key
     * @param key 键
     * @return List
     */
    public List get(K key){
        List list = map.get(key);
        if(list == null){
            list = createList();
        }
        return list;
    }

    /**
     * 获取第一个,对于只有一个值的来说
     */
    public V getFirst(K key){
        List list = map.get(key);
        return (null == list || list.size() == 0) ? null : list.get(0);
    }

    public Set keySet(){
        return map.keySet();
    }
    /**
     * clear ArrayListMultimap
     */
    public void clear(){
        map.clear();
    }

    /**
     * 从普通map转换而来
     */
    public static  ArrayListMultimap fromMap(Map map){
        if(null == map){
            return null;
        }
        ArrayListMultimap mapList = new ArrayListMultimap<>(map.size());
        map.forEach(mapList::put);
        return mapList;
    }
    /**
     * 从MultiValueMap转换而来
     */
    public static  ArrayListMultimap fromMap(MultiValueMap map){
        if(null == map){
            return null;
        }
        ArrayListMultimap mapList = new ArrayListMultimap<>(map.size());
        map.forEach((k1, v1s) -> v1s.forEach(v1 -> mapList.put(k1 , v1)));
        return mapList;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy