5
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?

Marcos Gonzalez
  • 1,086
  • 2
  • 12
  • 19
GraphicsToBe
  • 115
  • 4
  • 1
    Collection is a interface which is implemented by LinkedList. You should always point with an interface at the implementation. – emd Mar 19 '13 at 20:54

6 Answers6

7

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

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
2
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)

Community
  • 1
  • 1
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    @Aboutblank Array is not a collection and also Map is not a Collection :) – PermGenError Mar 19 '13 at 20:55
  • @PermGenError I didnt understand "because point 2 days so" partm can you re-phrase it or explain? – GraphicsToBe Mar 19 '13 at 21:01
  • @GraphicsToBe check it here http://stackoverflow.com/questions/340623/programming-to-an-interface-how-to-decide-where-its-needed – PermGenError Mar 19 '13 at 21:03
  • and here http://stackoverflow.com/questions/3194278/should-you-always-code-to-interfaces-in-java :) – PermGenError Mar 19 '13 at 21:04
  • 2
    The first case is not necessarily bad, you should probably explain why. – Perception Mar 19 '13 at 21:05
  • @Perception i already said it in my answer, It is bad because, asmany other class's also implement Collection interface in jav acollection framework. you don't want an reference variable referring to a list implementation to refer to an Set implemntation later in the code. – PermGenError Mar 19 '13 at 21:10
  • 2
    @PermGenError - yes, but that assumes that you care about list semantics vs collection semantics. In other words, thats a design time decision (aka, its not *always* bad to choose Collection over List). – Perception Mar 19 '13 at 21:12
  • @Perception hmm, but it is not generally accepted practice is it ? at least when compared to 3rd approach which is pretty accepted standard. :) – PermGenError Mar 19 '13 at 21:14
  • @Perception Collection and List are equally bad:) – ZhongYu Mar 19 '13 at 21:15
  • @zhong.j.yu do you mean programming to an interace is bad ? – PermGenError Mar 19 '13 at 21:16
  • 2
    @zhong.j.yu - sorry, I 100% disagree. Maybe you can make a convincing argument but I doubt it. – Perception Mar 19 '13 at 21:16
  • program to interfaces _at the API level_, not at the implementation level. – ZhongYu Mar 19 '13 at 21:17
  • 1
    I am down voting because of this `Collection list = new LinkedList(); //bad` as it's not bad. If all you care about is a `Collection`, then why does it matter if it's a `List`? Why would care if the `Collection ` is a `LinkedList`, `ArrayList`, or `HashSet`? – Steve Kuo Mar 19 '13 at 22:18
  • @SteveKuo so do you mean to say its good programming practice ?? – PermGenError Mar 19 '13 at 22:21
  • 1
    It's a good practice to program to the interface that you care about. If all I care about is operating on a `Collection`, and don't need linear indexing (`List`), uniqueness (`Set`), navigability (`NavigableSet`), sorted order (`SortedSet`), then why bother constraining to those interfaces? – Steve Kuo Mar 19 '13 at 22:28
  • I also don't understand why it's bad to refer to a `HashSet` – Steve Kuo Mar 19 '13 at 22:33
2

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.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
1

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.

axelrod
  • 3,782
  • 1
  • 20
  • 25
0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

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<?>.

ach
  • 6,164
  • 1
  • 25
  • 28