Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Collection better than a LinkedList?

Collection list = new LinkedList(); // Good?
LinkedList list = new LinkedList(); // Bad?

First variant gives more flexibility, but is that all? Are there any other reasons to prefer it? What about performance?

like image 437
GraphicsToBe Avatar asked Mar 19 '13 20:03

GraphicsToBe


People also ask

What is better than LinkedList?

Hence, the average case for accessing the element in a linked list is O(n). Thus, it is better to use an array if the requirement for accessing the elements.

Which is better LinkedList or stack?

Linked lists are good for inserting and removing elements at random positions. In a stack, we only ever append to or remove from the end which makes an ArrayList much more appealing to implement a stack.

Which is better LinkedList or ArrayList?

ArrayList is faster in storing and accessing data. LinkedList is faster in manipulation of data.

Why LinkedList is preferred?

The LinkedList provides constant time for add and remove operations. So it is better to use LinkedList for manipulation. ArrayList has O(1) time complexity to access elements via the get and set methods. LinkedList has O(n/2) time complexity to access the elements.


3 Answers

These are design decisions, and one size usually doesn't fit all. Also the choice of what is used internally for the member variable can (and usually should be) different from what is exposed to the outside world.

At its heart, Java's collections framework does not provide a complete set of interfaces that describe the performance characteristics without exposing the implementation details. The one interface that describes performance, RandomAccess is a marker interface, and doesn't even extend Collection or re-expose the get(index) API. So I don't think there is a good answer.

As a rule of thumb, I keep the type as unspecific as possible until I recognize (and document) some characteristic that is important. For example, as soon as I want methods to know that insertion order is retained, I would change from Collection to List, and document why that restriction is important. Similarly, move from List to LinkedList if say efficient removal from front becomes important.

When it comes to exposing the collection in public APIs, I always try to start exposing just the few APIs that are expected to get used; for example add(...) and iterator().

like image 142
Dilum Ranatunga Avatar answered Oct 07 '22 08:10

Dilum Ranatunga


Collection list = new LinkedList(); //bad

This is bad because, you don't want this reference to refer say an HashSet(as HashSet also implements Collection and so does many other class's in the collection framework).

LinkedList list = new LinkedList(); //bad?

This is bad because, good practice is to always code to the interface.

List list = new LinkedList();//good

This is good because point 2 days so.(Always Program To an Interface)

like image 23
PermGenError Avatar answered Oct 07 '22 07:10

PermGenError


Use the most specific type information on non-public objects. They are implementation details, and we want our implementation details as specific and precise as possible.

like image 2
ZhongYu Avatar answered Oct 07 '22 07:10

ZhongYu