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

net.dreamlu.utils.ArrayListMultimap Maven / Gradle / Ivy

package net.dreamlu.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自己实现的ArrayListMultimap 重复key的map,使用监听的type,取出所有的监听器
 * @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>();
	}

	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;
		}
	}

	/**
	 * 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;
	}

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy