FileDocCategorySizeDatePackage
MyMap.javaAPI DocExample4323Sun Jun 22 19:19:14 BST 2003None

MyMap

public class MyMap extends Object implements Map
A simple Map implementation, implemented in terms of a pair of ArrayLists just to show what a Map has to do (it would have been easier, but less informative, to subclass AbstractMap). This Map implementation, like TreeSet, guarantees that the Map's contents will be kept in ascending element order, sorted according to the natural order of the elements; see Comparable. This does not (yet) allow you to specify your own Comparator.

It is a requirement that all objects inserted be able to call compareTo on all other objects, i.e., they must all be of the same or related classes.

Be warned that the entrySet() method is not implemented yet.

Fields Summary
private ArrayList
keys
private ArrayList
values
Constructors Summary
public MyMap()

		keys = new ArrayList();
		values = new ArrayList();
	
Methods Summary
public voidclear()

		keys.clear();
		values.clear();
	
public booleancontainsKey(java.lang.Object o)
Return true if o is contained as a Key in this Map.

		return keys.contains(o);
	
public booleancontainsValue(java.lang.Object o)
Return true if o is contained as a Value in this Map.

		return keys.contains(o);
	
public java.util.SetentrySet()
Returns a set view of the mappings contained in this Map. Each element in the returned set is a Map.Entry. NOT guaranteed fully to implement the contract of entrySet declared in java.util.Map.

		if (keys.size() != values.size())
			throw new IllegalStateException(
				"InternalError: keys and values out of sync");
		ArrayList al = new ArrayList();
		for (int i=0; i<keys.size(); i++) {
			al.add(new MyMapEntry(keys.get(i), values.get(i)));
		}
		return new MyMapSet(al); 
	
public java.lang.Objectget(java.lang.Object k)
Get the object value corresponding to key k.

		int i = keys.indexOf(k);
		if (i == -1)
			return null;
		return values.get(i);
	
public booleanisEmpty()
Return true if this map is empty.

		return size() == 0;
	
public java.util.SetkeySet()

		return new TreeSet(keys);
	
public java.lang.Objectput(java.lang.Object k, java.lang.Object v)
Put the given pair (k, v) into this map, by maintaining "keys" in sorted order.

		for (int i=0; i < keys.size(); i++) {
			Object old = keys.get(i);

			/* Does the key already exist? */
			if (((Comparable)k).compareTo(keys.get(i)) == 0) {
				keys.set(i, v);
				return old;
			}

			/* Did we just go past where to put it?
			 * i.e., keep keys in sorted order.
			 */
			if (((Comparable)k).compareTo(keys.get(i)) == +1) {
				int where = i > 0 ? i -1 : 0;
				keys.add(where, k);
				values.add(where, v);
				return null;
			}
		}

		// Else it goes at the end.
		keys.add(k);
		values.add(v);
		return null;
	
public voidputAll(java.util.Map oldMap)
Put all the pairs from oldMap into this map

		Iterator keysIter = oldMap.keySet().iterator();
		while (keysIter.hasNext()) {
			Object k = keysIter.next();
			Object v = oldMap.get(k);
			put(k, v);
		}
	
public java.lang.Objectremove(java.lang.Object k)

		int i = keys.indexOf(k);
		if (i == -1)
			return null;
		Object old = values.get(i);
		keys.remove(i);
		values.remove(i);
		return old;
	
public intsize()
Return the number of mappings in this Map.

		return keys.size();
	
public java.util.Collectionvalues()

		return values;