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

org.snpeff.collections.MultivalueHashMap Maven / Gradle / Ivy

The newest version!
package org.snpeff.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * A Hash that can hold multiple values for each key
 *
 * @author pcingola
 */
public class MultivalueHashMap extends HashMap> {

	private static final long serialVersionUID = -8860279543165990227L;

	public MultivalueHashMap() {
		super();
	}

	/**
	 * Add multiple values
	 */
	public void add(K key, Collection values) {
		getOrCreate(key).addAll(values); // Add all to the list
	}

	/**
	 * Add a single value
	 */
	public void add(K key, V value) {
		getOrCreate(key).add(value); // Add to the list
	}

	/**
	 * Get a list of values (or create it if not available)
	 */
	public List getOrCreate(K key) {
		// Get list
		LinkedList list = get(key);
		if (list == null) { // No list? Create one
			list = new LinkedList();
			put(key, list);
		}
		return list;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy