GraphicsToBe
GraphicsToBe

Reputation: 115

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?

Upvotes: 5

Views: 200

Answers (6)

ZhongYu
ZhongYu

Reputation: 19682

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.

Upvotes: 2

PermGenError
PermGenError

Reputation: 46428

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)

Upvotes: 2

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

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().

Upvotes: 7

ach
ach

Reputation: 6234

My general rule is to only be as specific as you need to be at the time (or will need to be in the near future, within reason). Granted, this is somewhat subjective.

In your example I would usually declare it as a List just because the methods available on Collection aren't very powerful, and the distinction between a List and another Collection (Map, Set, etc.) is often logically significant.

Also, in Java 1.5+ don't use raw types -- if you don't know the type that your list will contain, at least use List<?>.

Upvotes: 0

axelrod
axelrod

Reputation: 3978

Sure. If for example java will find and implement more efficient implementation for the List collection, but you already have API that accepts only LinkedList, you won't be able to replace the implementation if you already have clients for this API. If you use interface, you can easily replace the implementation without breaking the APIs.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93668

They're absolutely equivalent. The only reason to use one over the other is that if you later want to use a function of list that only exists in the class LinkedList, you need to use the second.

Upvotes: 0

Related Questions