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

aima.core.util.datastructure.TwoKeyHashMap Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

There is a newer version: 3.0.0
Show newest version
package aima.core.util.datastructure;

import java.util.HashMap;

/**
 * Provides a hash map which is indexed by two keys. In fact this is just a hash
 * map which is indexed by a pair containing the two keys. The provided two-key
 * access methods try to increase code readability.
 * 
 * @param 
 *            First key
 * @param 
 *            Second key
 * @param 
 *            Result value
 * 
 * @author Ruediger Lunde
 * @author Mike Stampone
 */
public class TwoKeyHashMap extends HashMap, V> {
	private static final long serialVersionUID = -2232849394229644162L;

	/**
	 * Associates the specified value with the specified key pair in this map.
	 * If the map previously contained a mapping for this key pair, the old
	 * value is replaced.
	 * 
	 * @param key1
	 *            the first key of the key pair, with which the specified value
	 *            is to be associated.
	 * @param key2
	 *            the second key of the key pair, with which the specified value
	 *            is to be associated.
	 * @param value
	 *            the value to be associated with the key pair.
	 * 
	 */
	public void put(K1 key1, K2 key2, V value) {
		super.put(new Pair(key1, key2), value);
	}

	/**
	 * Returns the value to which the specified key pair is mapped in this two
	 * key hash map, or null if the map contains no mapping for
	 * this key pair. A return value of null does not necessarily
	 * indicate that the map contains no mapping for the key; it is also
	 * possible that the map explicitly maps the key to null. The
	 * containsKey method may be used to distinguish these two
	 * cases.
	 * 
	 * @param key1
	 *            the first key of the key pair, whose associated value is to be
	 *            returned.
	 * @param key2
	 *            the second key of the key pair, whose associated value is to
	 *            be returned.
	 * 
	 * @return the value to which this map maps the specified key pair, or
	 *         null if the map contains no mapping for this key
	 *         pair.
	 */
	public V get(K1 key1, K2 key2) {
		return super.get(new Pair(key1, key2));
	}

	/**
	 * Returns true if this map contains a mapping for the
	 * specified key pair.
	 * 
	 * @param key1
	 *            the first key of the key pair whose presence in this map is to
	 *            be tested.
	 * @param key2
	 *            the second key of the key pair whose presence in this map is
	 *            to be tested.
	 * 
	 * @return true if this map contains a mapping for the
	 *         specified key.
	 */
	public boolean containsKey(K1 key1, K2 key2) {
		return super.containsKey(new Pair(key1, key2));
	}

	/**
	 * Removes the mapping for this key pair from this map if present.
	 * 
	 * @param key1
	 *            the first key of the key pair, whose mapping is to be removed
	 *            from the map.
	 * @param key2
	 *            the second key of the key pair, whose mapping is to be removed
	 *            from the map.
	 * 
	 * @return the previous value associated with the specified key pair, or
	 *         null if there was no mapping for the key pair. A
	 *         null return can also indicate that the map
	 *         previously associated null with the specified key
	 *         pair.
	 */
	public V removeKey(K1 key1, K2 key2) {
		return super.remove(new Pair(key1, key2));
	}

	// public static void main(String[] args) {
	// TwoKeyHashMap hash = new TwoKeyHashMap();
	// hash.put("A", "A", "C");
	// hash.put("A", "A", "D");
	// hash.put("B", "A", "E");
	// System.out.println(hash.get("A", "A"));
	// System.out.println(hash.get("A", "B"));
	// }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy