import java.util.*; public class Map extends List { public int size() { return(N); } public boolean isEmpty() { return(N==0); } public ValueType get(KeyType k) { ListElem p; for ( p = head; p != null; p = p.next ) { if ( p.key.equals( k ) ) return( p.value ); } return(null); } public ValueType put(KeyType k, ValueType v) { ListElem p; ValueType old; for ( p = head; p != null; p = p.next ) { if ( p.key.equals( k ) ) { old = p.value; p.value = v; // Replace old value return( old ); } } /* ------------------------------- Not found, insert new entry ------------------------------- */ p = new ListElem (k, v); insert(p); return(null); } public ValueType remove(KeyType k) { ListElem p; ValueType old; for ( p = head; p != null; p = p.next ) { if ( p.key.equals( k ) ) { old = p.value; delete(p); // Delete element return(old); // Return old value } } return(null); // Return "not found" } public Iterable keySet() { LinkedList r = new LinkedList(); // One of the classes that implements // "Iterable" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p.key ); } return(r); } public Iterable values() { LinkedList r = new LinkedList(); // One of the classes that implements // "Iterable" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p.value ); } return(r); } public Iterable> entrySet() { LinkedList> r = new LinkedList>(); // One of the classes that implements // "Iterable" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p ); } return(r); } }