Dictionary ADT

Dictionary

Dictionary: Organize data as key-value pairs (k,v).

Specification

Dictionaryadd(K : key, V: value) : Vremove(K : key) : VgetValue(K : key) : Vcontains(K : key) : booleangetKeyIterator() : Iterator<K>getValueIterator() : Iterator<V>isEmpty() : booleangetSize() : intclear() : void
PseudocodeDefinition
addAdd k,v pair. Returns null if new entry was added. If an entry with the same key already exists, it’ll replace the pair and return the old value.
removeRemove an entry by its key. Returns value of removed pair, or null if key wasn’t found.
getValueGet the value at a key
containsCheck whether the dictionary contains a key
getKeyIteratorReturns key iterator
getValueIteratorReturns value iterator
isEmptyGets whether dictionary is empty
getSizeGets number of entries in dictionary
clearRemoves all entries from dictionary

Limitations: This specification cannot support null keys or values.

Performance Note:

Example: Dictionary Interface

public interface DictionaryInterface<K,V> {
  V add(K key, V value);
  V remove(K key);
  V getValue(K key);
  boolean contains(K key);
  Iterator<K> getKeyIterator();
  Iterator<V> getValueIterator();
  boolean isEmpty();
  int getSize();
  void clear();
}

Example: Using dictionary for telephone directory

TelephoneDirectoryphonebookreadFile(data)getPhoneNumber(name)Dictionary(store as <Name,String> pairs)Name(provide methods to work with names)

Example: Word Concordance and Count

  1. Word Count
  1. Word Concordance

Java Class Library: The Interface Map

V put(K key, V value);
V remove(Object key);
V get(Object key);
boolean containsKey(Object key);
boolean containsKey(Object value);
Set<K> keySet();
Collection<V> values();
boolean isEmpty();
int size();
void clear();

Implementation

Array-Based

  1. You could use array of objects, where each object encapsulates a key and value pair.

Example: The sort of wacky casting you need to do to create an generic array of Entry objects that take a key,value pair

@SuppressWarnings("unchecked")
Entry<K,V>[] tempDictionary = (Entry<K,V>[]) new Entry[initialCapacity]
this.dictionary = tempDictionary

Example: An inner-class to store key,value pairs

private class Entry<K,V> {
  private K key;
  private V value;
  private Entry(K key, V value) {
      this.key = key;
      this.value = value;
  }
  /** etc... */
}

Note: To create a new Entry, do new Entry<>(key, value);—Java will figure the contents of the square brackets by itself.

  1. Or, you could use two arrays in parallel, one array for key and another array for value.

Note: If your arrays are unsorted, every single operation (addition, removal, retrieval, traversal) will take O(n)

On Sorting Arrays: If you sort on-the-go (by ensuring that you maintain order while adding and removing), you’ll still be shifting data around.

Linked List

  1. You can use a linked list, where each node’s data is a key,value pair
  2. You could also use two linked lists, one list for keys and another for values.
  3. Or, we could have each node have two data fields, one for key and another for value.

Note: Despite this an unsorted linked dictionary will still have addition, removal, retrieval, and traversal take O(n).

On Sorting Linked List:

Example: Private key-value node inner class

private class Node {
private K key;
private V value;
private Node next;
/** etc. */
}

Example: Header for a sorted linked implementation

public class SortedLinkedDictionary<K extends Comparable<? super K>, V> implements DictionaryInterface<K,V>

Thus: A sorted array is the only implementation covered so far that provides better performance.