import java.util.*; public class ListMap 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 Collection keySet() { LinkedList r = new LinkedList(); // One of the classes that implements // "Collection" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p.key ); } return(r); } public Collection values() { LinkedList r = new LinkedList(); // One of the classes that implements // "Collection" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p.value ); } return(r); } public Collection> entrySet() { LinkedList> r = new LinkedList>(); // One of the classes that implements // "Collection" is LinkedList ListElem p; for ( p = head; p != null; p = p.next ) { r.add( p ); } return(r); } }