简体   繁体   中英

Linked list class from scratch vs Default linked list class?

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?

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:

  1. Nobody will ever need to maintain the implementation of the LinkedList .
  2. Any future additions to the List interface will be automatically inherited.
  3. Developers reading your code will immediately understand what is going on.
  4. The Collections API has a ton of useful utility methods that will become available.
  5. Your 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM