Search code examples
javalistcollectionsinterface

Why in List interface only add(int index, E element) returns void wheras all other add methods return boolean


Don't get me wrong - I understand why add methods deriving from Collection must return boolean, I just don't get why there is inconsistency with this one method - addAll both return boolean even though one of them doesn't have to.


Solution

  • The boolean return value is there to help you catch the case where you give it an empty list. The add method will always change your list so the return value would just always be true.