0

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.

Gregor D
  • 131
  • 1
  • 5
  • 2
    Did you find out what `m` is, and look for the source code of `m`'s class's `containsKey` method? – rgettman Jan 21 '15 at 00:39
  • Ok sry, I feel dumb now. m is a NavigableMap but it is instantiated as TreeMap and there I did find the implementation. Should I delete this question? – Gregor D Jan 21 '15 at 00:45

1 Answers1

1

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).

Todd
  • 30,472
  • 11
  • 81
  • 89
  • The `TreeSet(NavigableMap m)` constructor is package-only scoped, so it can't just be any `NavigableMap` - it's only possible to construct a `TreeSet` with a `TreeMap`, or ones constructed by `TreeSet` from submaps of `TreeMap` – Erwin Bolwidt Jan 21 '15 at 00:50
  • Yeah you are right. I missed that NavigableMap m could actually be the superinterface of a class implementing it... In eclipse I clicked on m and then "Open Declaration" but then the Interface of NavigableMap opened ... I don't know why I didn't think of it myself.. I should go to bed – Gregor D Jan 21 '15 at 00:53
  • 1
    Sometimes you just need another pair of eyes on things. Happens to everybody. – Todd Jan 21 '15 at 00:53