Reputation: 644
I've been writing my linked list data structure from scratch as a student for projects/assignments and I was wondering in the "real-world" does a developer have to write their own linked list DS or use any of the already provided linked list object in the Java docs. Which is better & in what scenario?
Upvotes: 4
Views: 692
Reputation: 140467
Very simple rule: do not re-invent the wheel unless you have very good reasons to do so.
Meaning: when you are doing "professional" work; you always balance
A) re-using existing componentry
versus
B) creating your own solutions.
And especially for anything that is "collection" oriented there are very few good reasons to completely redo something.
Thus, the "technical" answer is: for production work you have to assess cost/benefit of both of those alternatives; and then you decide what to do. But especially for classes that come out of the Java standard libraries, you should prefer re-use. You depend on those libraries anyway; so there is no point in not using them.
And to give some things that would come into consideration:
A) gain from writing your own components: you avoid dependencies on 3rd party code. You might be able to create something that works perfectly within your setup. ...
B) cost of doing so: of course, the direct cost for creating and maintaining your own solution over time (esp. maintenance is often heavily underestimated! And of course, all those points that "castle" listed in his answer.
Upvotes: 3
Reputation: 33496
A custom LinkedList
implementation will very likely be more efficient because it allows you to make optimizations for your needs, however the JDK's LinkedList
will always be preferable for its re-usability and maintainability benefits:
LinkedList
.List
interface will be automatically inherited.LinkedList
will be compatible with many third-party libraries which use List
.If you do decide to create a custom List
implementation, you may choose to extend AbstractSequentialList
in order to gain the benefits of #2, #4, and possibly #5.
Upvotes: 6