Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry - Common Java Functionality Copyright (c) 2012-2015, Sandeep Gupta http://sangupta.com/projects/jerry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
  
 package com.sangupta.jerry.ds;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
A very simple implementation of a Multi-Map for use that does not implement the java.util.Map interface. It is useful for situations where a very light-weight implementation is needed, for collecting and iterating over multiple objects that map to same key. This implementation is based on java.util.concurrent.ConcurrentHashMap. For more advanced implementations see Google Guava library. The implementation is not thread-safe multiple threads are adding values for a key that is also being removed.

Author(s):
sangupta
 
 public class SimpleMultiMap<K, V> {

 
 	private final ConcurrentMap<K, List<V>> map = new ConcurrentHashMap<K, List<V>>();

Return the number of keys stored in the map.

Returns:
the size of map
 
 	public int size() {
 		return this..size();
 	}

Check if there are any keys stored in this map or not

Returns:
true if no keys are present, false otherwise
 
 	public boolean isEmpty() {
 		return this..isEmpty();
 	}

Check if the given key is present in the map or not

Parameters:
key the key being looked for
Returns:
true if key is present, false otherwise
 
 	public boolean containsKey(K key) {
 		return this..containsKey(key);
 	}

Return the list of all values stored against the key

Parameters:
key the key being looked for
Returns:
the list of all values, null otherwise
 
 	public List<V> getValues(K key) {
 		return this..get(key);
 	}

Store the given value object against the key.

Parameters:
key the key for mapping
value the value to be stored
	public void put(K key, V value) {
		List<V> values;
		if(this..containsKey(key)) {
			values = this..get(key);
			if(values != null) {
				values.add(value);
				return;
			}
		}
		// no value
		values = new ArrayList<V>();
		values.add(value);
		values = this..putIfAbsent(keyvalues);
		if(values == null) {
			// added successfully
			return;
		}
		values.add(value);
	}

Remove and return all values associated with the given key.

Parameters:
key
Returns:
	public List<V> remove(K key) {
		return this..remove(key);
	}

Return a java.util.Set of all keys in this map.

Returns:
	public Set<K> keySet() {
		return this..keySet();
	}

Clear all keys from this map.
	public void clear() {
		this..clear();
	}
	public int hashCode() {
		return this..hashCode();
	}
	public boolean equals(Object obj) {
		return this..equals(obj);
	}
New to GrepCode? Check out our FAQ X