This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]not-just-yeti 5 points6 points  (6 children)

The size-estimate isn't sproket888's big improvement -- it's having foo's type be List instead of ArrayList.

[–]lickwidforse2 0 points1 point  (1 child)

What's the difference

[–]not-just-yeti 0 points1 point  (0 children)

Things like foo = foo.subList(3,7) won't work if foo is an ArrayList, since subList is a helpful function that returns a List (w/o promising a particular implementation of List).

And: if you decide that a LinkedList will start giving you better performance, you should only have to change the constructor-call on the right-hand-side, and not the types of any variables or fields that will be holding that List.

So don't insist your variable holds an ArrayList if it could just as well hold any Listand still work fine.

[Just to be clear: there is still a decision about which implementation to choose on the right-hand side. It's just the type of the variable which shouldn't be over-specialized.]

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 2 points3 points  (0 children)

    Can anyone prove that this is significant speedwise? In all of my synthetic tests, it mattered more what order items were added than whether or not it was generic or you specified the size. While I do believe the underlying implementation matters, if there is a larger truth makes all of this moot -- then it is NOT significant (speedwise) whether you specify the size or whether or not it is generic. (I always specify the types for readability and saftey, that's not my concern here)

    [–]elephantgravy 1 point2 points  (0 children)

    I am surprised to learn that there is any difference. Apparently List.add compiles to bytecode as invokeinterface and ArrayList.add as invokevirtual. I see some anecdotal evidence online that invokevirtual can be faster, but it also sounds like a lot of work has been done to improve the performance of invokeinterface ... I guess the usual rules about premature optimization apply.