I\'m wondering what is the time complexity of size()
for portion view of TreeSet.
Let say I\'m adding random numbers to set (and I do not care about dup
Looks like complexity of size ()
is O(N)
, because it may call TreeMap.NavigableSubMap.EntrySetView.size ()
which is implemented like this (Oracle JDK 1.7.0_13):
public int size() {
if (fromStart && toEnd)
return m.size();
if (size == -1 || sizeModCount != m.modCount) {
sizeModCount = m.modCount;
size = 0;
Iterator i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
}
return size;
}