structure5
Class MapList<K,V>

java.lang.Object
  extended by structure5.MapList<K,V>
All Implemented Interfaces:
Map<K,V>

public class MapList<K,V>
extends java.lang.Object
implements Map<K,V>

Associations establish a link between a key and a value. An associative array or map is a structure that allows a disjoint set of keys to become associated with an arbitrary set of values. The convenience of an associative array is that the values used to index the elements need not be comparable and their range need not be known ahead of time. Furthermore, there is no upper bound on the size of the structure. It is able to maintain an arbitrary number of different pieces of information simultaneously. Maps are sometimes called dictionaries because of the uniqueness of the association of words and definitions in a household dictionary.

This implementation is based on a list, so performance for most operations is linear.

Example Usage:

To create a dictionary by reading a collection of words and definitions from System.in we could use the following!

 public static void main (String[] argv){
      Map dict = new MapList();
      ReadStream r = new ReadStream();
      String word, def;
      System.out.println("Enter a word: ");
      while(!r.eof()){
          word = r.readLine();
          System.out.println("Enter a definition: ");
          def = r.readLine();
          dict.put(word,def);
          System.out.println("Enter a word: ");
      }
      System.out.println(dict);
 }
 


Field Summary
protected  List<Association<K,V>> data
          List for storing the entries in this map
 
Constructor Summary
MapList()
          Construct an empty map, based on a list
MapList(Map<K,V> source)
          Construct a map with values found in source
 
Method Summary
 void clear()
           
 boolean containsKey(K k)
           
 boolean containsValue(V v)
           
 Set<Association<K,V>> entrySet()
           
 boolean equals(java.lang.Object other)
           
 V get(K k)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
           
 V put(K k, V v)
           
 void putAll(Map<K,V> other)
           
 V remove(K k)
           
 int size()
          Returns the number of entries in the map
 Structure<V> values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

protected List<Association<K,V>> data
List for storing the entries in this map

Constructor Detail

MapList

public MapList()
Construct an empty map, based on a list

Postcondition:
constructs an empty map, based on a list

MapList

public MapList(Map<K,V> source)
Construct a map with values found in source

Postcondition:
constructs a map with values found in source
Method Detail

size

public int size()
Returns the number of entries in the map

Specified by:
size in interface Map<K,V>
Postcondition:
returns the number of entries in the map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Postcondition:
returns true iff this map does not contains any entries

containsKey

public boolean containsKey(K k)
Specified by:
containsKey in interface Map<K,V>
Precondition:
k is non-null
Postcondition:
returns true iff k is a key that is mapped to a value; that is, k is in the domain of the map

containsValue

public boolean containsValue(V v)
Specified by:
containsValue in interface Map<K,V>
Precondition:
v is non-null
Postcondition:
returns true iff v is the target of at least one map entry; that is, v is in the range of the map

get

public V get(K k)
Specified by:
get in interface Map<K,V>
Precondition:
k is a key, possibly in the map
Postcondition:
returns the value mapped to from k, or null

put

public V put(K k,
             V v)
Specified by:
put in interface Map<K,V>
Precondition:
k and v are non-null
Postcondition:
inserts a mapping from k to v in the map

remove

public V remove(K k)
Specified by:
remove in interface Map<K,V>
Precondition:
k is non-null
Postcondition:
removes any mapping from k to a value, from the mapping

putAll

public void putAll(Map<K,V> other)
Specified by:
putAll in interface Map<K,V>
Precondition:
other is non-null
Postcondition:
all the mappings of other are installed in this map, overriding any conflicting maps

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Postcondition:
removes all map entries associated with this map

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Postcondition:
returns a set of all keys associated with this map

values

public Structure<V> values()
Specified by:
values in interface Map<K,V>
Postcondition:
returns a structure that contains the range of the map

entrySet

public Set<Association<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Postcondition:
returns a set of (key-value) pairs, generated from this map

equals

public boolean equals(java.lang.Object other)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class java.lang.Object
Precondition:
other is non-null
Postcondition:
returns true iff maps this and other are entry-wise equal

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class java.lang.Object
Postcondition:
returns a hash code associated with this structure