Anonymous Inner Classes

One more thing: let me teach you anonymous inner classes. If you are looking at others code (like Josh's when you look at TreeMap's source code) you might see this and be confused. An anonymous inner class is a way of shorthand-ing the creation of an inner class or the implementation of an interface. The syntax looks like this:
public Set<Map.Entry<K, V>> entrySet() {

  return new AbstractSet<Map.Entry<K, V>>() {
    public boolean add(final Object o) {
      throw new UnsupportedOperationException();
    }
  };
}

Notice two things--first, we are sort of ``instantiating'' an abstract class (we could do the same with an interface), but instead of a semicolon we open a curly brace and begin defining its methods. The compiler will not let you compile this code unless you implement all of the methods in Set within the braces (for obvious reasons). What actually happens behind the scenes is that the compiler changes that statement into code that looks like this:

// Generics don't exist once the compiler has its say; see Part 1
public Set entrySet() { return new AnonymousInnerClass1(); }
public class AnonymousInnerClass1 implements Set {
  public boolean add(Object o) {
    throw new UnsupportedOperationException();
  }
}

Pretty handy, huh?

The source code for a full implementation of SortedMap using the skiplist is in the SortedMapExample.jar file in the top level cmsc420 directory in my user pages. And, you also have the TreeMap's source code) to study. Again, you are to replace the use of the SkipList in our example file and the use of the Red-Black tree in built-in TreeMap and TreeSet implementations. You will lose full credit if the DodekaTrie is not used appropriately.

MM Hugue 2017-09-24