aspose file tools*
The moose likes Java in General and the fly likes List Vs. ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List Vs. ArrayList" Watch "List Vs. ArrayList" New topic
Author

List Vs. ArrayList

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List Vs. ArrayList