import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LinkedContainerMap<K, V>{
	private Map<K, Set<V>> map = new HashMap<K, Set<V>>();
	private List<K> list = new ArrayList<K>();
	
	public synchronized void clear() {
		map.clear();
		list.clear();
	}
	
	public synchronized void put(K key, V value)
	{
		Set<V> oldValue = map.get(key);
		if (oldValue == null)
		{
			oldValue = new HashSet<V>();
			map.put(key, oldValue);
			list.add(key);
		}
		
		oldValue.add(value);
	}
		
	public synchronized K popKey()
	{
		if (list.size() == 0)
		{
			return null;
		}
		
		K key = list.get(0);
		list.remove(0);
		
		return key;
	}
	
	public synchronized Set<V> popVaue(K key)
	{
		Set<V> value = map.get(key);
		map.remove(key);
		
		return value;
	}
	
	public synchronized int totalSize()
	{
		return map.size();
	}
	
	public synchronized int unassignedSize()
	{
		return list.size();
	}
}
