> Why...
>
>List test = new ArrayList();
This is not a Yahoo chat room. If you expect to get any sort of answer other than one that you probably won't like then read this:
http://www.catb.org/~esr/faqs/smart-questions.html
Then explain what your problem is with the code you posted.
The 3 main collections which implement List are Vector, ArrayList and LinkedList. With the code snippet you posted you can easily change the right-hand side value to say a Vector or LinkedList later on with only a small change to your code (ie the right-hand side of your snip). If you have a method for iterating over a List in your application then this would not need to be changed because you will still be iterating over a List even after the change to the right-hand side value. It gives you flexibility in your applications . You lose out on the implementation specific methods but a lot of the time this is not a major problem.
You can also use
Collection c = new ArrayList();
This would allow you to change to any collection which implements the Collection interface.
Check out this link for more details
http://www.informit.com/isapi/product_id~%7B7DE69993-3EF5-4354-9E10-9F8A535909F1%7D/content/images/0201310058/samplechapter/blochch7.pdf
Message was edited by:
_helloWorld_
> so, we can say that the best and efficient way...
Not by a long shot.
Read these:
http://www.artima.com/lejava/articles/designprinciplesP.html
http://www.informit.com/isapi/product_id~%7B7DE69993-3EF5-4354-9E10-9F8A535909F1%7D/content/images/0201310058/samplechapter/blochch7.pdf (Item #34, mentioned previously)
~
> thanks man!
> :)
>
>so, we can say that the best and efficient way is
>ArrayList test = new ArrayList();
No.
And efficiency has nothing to do with it.
> and, why would i use it in a big project for
> example? (List = new ArrayList())
Why would you use what? ArrayList? That particular construct? What do you mean "in a big project"? The size of the project has nothing to do with it. You use the constructs that express your design intent most directly and accurately.
Seriously, you need to ask more specific, directed questions.
> thanks man!
> :)
>
>so, we can say that the best and efficient way is
>ArrayList test = new ArrayList();
>
> and, why would i use it in a big project for
> example? (List = new ArrayList())
No. Most people will always recommend that you program to the interface and not the implementation. Even if you are working on a small scale (I take it you are learning and probably working in the main method) then this is a good habit to get into from the beginning.
Take a look at the API for List and then for ArrayList. You will see that most of the methods are the same, bar a few that you don't need very often.
> What is the most efficient way?
Most efficient way to do what?
More efficient that what alernatives?
Most efficient in terms of what? CPU cycles? Memory? Total cost to bring to production?
You're asking very vague questions and worrying too much about "efficiency" rather than writing correct, easy-to-understand, maintainable code that meets your requirements.
>
> in my opinion it was
>
> ArrayList<TYPE> test = new ArrayList<TYPE>();
Why do you think that is more "effiicient" that some other way of doing whatever it is you're trying to do?
> Why do you think that is more "effiicient" that some
> other way of doing whatever it is you're trying to do?
Well, jverd, the thing he was hoping to do and the thing he actually did, with regard to the other thing, may - in fact - have been the better thing given the actual things that were given at that particular point in time; considering all things to be equal and whatnot, the other thing may be as important as something else entirely different from the original. It's so obvious, don't you see?
... which reminds me - I was thinking of you last night ... I gave away nearly 50 pounds of venison to a friend. We were all "venisoned" out (my wife and I) and decided it was time to clean out the freezer. Suppose it'll be full again in a few months, though...
Message was edited by:
Navy_Coder
imagine you in a situation that u want
u only want to know about ur program be fast.. cause u need this in this context
only imagine that... so im only trying to redefine some old techniques... i've find (and test and is really true) that a hashset can be 114 times most efficient that arraylists.. this inside a loop of 300000 objects creation
its only tests of performace... i think that is no slow java program.. but bad programing
> imagine you in a situation that u want
>
> u only want to know about ur program be fast.. cause
> u need this in this context
>
> only imagine that... so im only trying to redefine
> some old techniques... i've find (and test and is
> really true) that a hashset can be 114 times most
> efficient that arraylists.. this inside a loop of
> 300000 objects creation
>
> its only tests of performace... i think that is no
> slow java program.. but bad programing
Sorry, dude. You're making zero sense.
> whats your doubt in my question? kkk
It's not specific enough.
It makes no sense.
It's focussing on the wrong thing.
I don't even know if you have a question. You seem to just be saying that HashSet is more efficient than ArrayList for "object creation," whatever that means.
Here's a suggestion: Pretend that we're not inside your head, and hence not privy to your entire thought process.
> List test = new ArrayList();
>
> This is the most efficient way because it is the
> shortest.
My codpiece it is! Creating a new ArrayList hammers your efficiency quotient something chronic. Just declare the reference, much more efficient
List test; // even shorter
> Instead of
> ArrayList test= new ArrayList();
> or
> List test = new ArrayList();
> or
> ArrayList<TYPE> test = new ArrayList<TYPE>();
>
>
> How about this:
>
> List<TYPE> test = new ArrayList<TYPE>();
With respect to what?
> It uses generics, so it a strongly typed collection,
> and it hast 'List' on the right hand side, so it uses
> an interface, allowing the right hand side of the
> statement to be changed if a better collection needs
> to be used later.
What if that "better" collection turns out to be a HashSet?