public Set entrySet() { return new Set() { public boolean add(Object o) { throw new UnsupportedOperationException(); } }; }
Notice two things--first, we are sort of ``instantiating'' an interface (we could do the same with an abstract class), 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:
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.
MM Hugue 2018-06-11