Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring a Collection

 
Nigel Foley
Greenhorn
Posts: 6
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was reading about collections tonight and came across the following code to declare an ArrayList, I have a few questions and I am hoping that someone might be able to help me out





This first example uses the ArrayList class and the second the List interface, what are the pros and cons for each approach?

Any info / links would be greatly appreciated

Thanks
Nigel
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally the second one is preferred.
You can use the object according to the List interface, and if you once happen to decide to use a LinkedList instead of the ArrayList, you can switch it without much rewriting.



 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Foley wrote:Hi All,

I was reading about collections tonight and came across the following code to declare an ArrayList, I have a few questions and I am hoping that someone might be able to help me out





This first example uses the ArrayList class and the second the List interface, what are the pros and cons for each approach?

Any info / links would be greatly appreciated

Thanks
Nigel


Polymorphism is the difference.
Read about it.
the methods that you can call though the ArrayList and List reference can be different.
 
Winston Gutkowski
Bartender
Pie
Posts: 10110
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:Polymorphism is the difference.

Actually, it isn't - except in the general sense that any interface method is called polymorphically.

@Nigel: The reason that, as Ivan says, the 2nd method is better is that it doesn't tie your program to a specific implementation. When you're designing something, you don't say "I want an ArrayList of things" (at least you shouldn't), you say "I want a List of things" - and the 2nd declaration does exactly that.

It's called "programming to the interface", and it's a very good habit to get into, especially when dealing with classes of the Java Collections Framework.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48430
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there is a better way still, which is to use generics. Note this particular idiom only works in Java7:-Before Java7, you wrote
 
Nigel Foley
Greenhorn
Posts: 6
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks for all the responses and links, its makes much more sense now

Nigel
 
Campbell Ritchie
Sheriff
Posts: 48430
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic