java - Why does AbstractCollection not implement size()? -



java - Why does AbstractCollection not implement size()? -

when sub-classing abstractcollection, must still implement size(), though (i believe) there reasonable right (though non-performant) default implementation:

public int size() { int count = 0; (iterator<e> = iterator(); i.hasnext();) { i.next(); count++ } homecoming count; }

why did designers not include default implementation of size()? trying forcefulness developers consciously think method, causing developer offer implementation performs improve default?

i suspect lastly sentence real reason. when subclassing abstract class it's tempting override abstract methods. expect every implementation have improve implementation iterating - if want pretty much override method, it's thought not provide base of operations (slow) implementation. reduces chances of screwing :)

java collections size

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -