• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List Vs. ArrayList

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a conventional method

Now my problem is why it is so. Why it is not a convention to use an Object of ArrayList directly it will also give the new methods of ArrayList Class which are not present in the List Class. I know that ArrayList implements List.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you are just using it for your class... sure... use the ArrayList object directly.

But if you are passing it to other classes, why expose something if you don't have to? In the future, if you have to change it to say, a LinkedList, for performance reasons, you only need to change it in one location.

Actually, now that I think of it, using List is a good idea, even if it is confined to a single class. If the methods of the class only deal with List, you can change the instantiation from ArrayList to LinkedList without affecting much, if any, code.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is called "programming to interfaces." If you write your whole program this way -- including method arguments of type List instead of ArrayList, and return types of List instead of ArrayList -- then you're free to change to any other implementation of List by changing only one line of code. As different implementations have different performance characteristics, this flexibility is a good thing.

Looking at this more broadly, you can and should use this with your own classes as well. Keeping your subsystems isolated via interfaces helps make them more robust and flexible. It also helps immensely in testing: being able to substitute different types makes it easier to test individual classes in isolation.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, if you're doing the inversion-of-control/dependency-injection dance, you tend to avoid creating key objects by explicitly calling their constructors (ArrayList probably doesn't qualify here as a key object). The pattern would have you write something like the following:
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
This is called "programming to interfaces." If you write your whole program this way -- including method arguments of type List instead of ArrayList, and return types of List instead of ArrayList -- then you're free to change to any other implementation of List by changing only one line of code. As different implementations have different performance characteristics, this flexibility is a good thing.

Looking at this more broadly, you can and should use this with your own classes as well. Keeping your subsystems isolated via interfaces helps make them more robust and flexible. It also helps immensely in testing: being able to substitute different types makes it easier to test individual classes in isolation.


Quoted because it was put eloquently and is advice the OP should heed!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic