Search code examples
javatreesetlanguage-implementation

Java's TreeSet implementation of contains (Sourcecode) missing


When I open the sourcecode of the TreeSet Class there is the following code:

public boolean contains(Object o) {
    return m.containsKey(o);
}

m is a NavigableMap which is an interface. So where is the implementation? It is definitely not in TreeSet itself.


Solution

  • From the source for TreeSet:

    TreeSet(NavigableMap<E,Object> m) {
        this.m = m;
    }
    
    public TreeSet() {
        this(new TreeMap<E,Object>());
    }
    

    So m should be a TreeMap (or possibly another implementation of NavigableMap if another class in the same package calls that constructor).