O.O. doubts!

Why...List test = new ArrayList();?
[56 byte] By [Dannemanna] at [2007-11-27 9:22:05]
# 1

> 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_

_helloWorld_a at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 2
It's called: Abstraction. Look it up.
Navy_Codera at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks man!:)so, we can say that the best and efficient way isArrayList test = new ArrayList();?and, why would i use it in a big project for example? (List = new ArrayList())
Dannemanna at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 4

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

~

yawmarka at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 5
>so, we can say that the best and efficient way is>ArrayList test = new ArrayList();Not even close.
Navy_Codera at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 6
What is the most efficient way?in my opinion it was ArrayList<TYPE> test = new ArrayList<TYPE>();
Dannemanna at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 7

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

jverda at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 8

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

_helloWorld_a at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 9
What is the most efficient way?in my opinion it wasArrayList<TYPE> test = new ArrayList<TYPE>();
Dannemanna at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 10
NOthe MOST EFFICIENT WAYCollection test = new HashSet():
Dannemanna at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 11
> What is the most efficient way?> > in my opinion it was> > ArrayList<TYPE> test = new ArrayList<TYPE>();See replies 2, 4 and 7.
Navy_Codera at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 12

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

jverda at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 13
opsthatCollection<TYPE> test = new HashSet<TYPE>(); the most efficient way == the most fst way
Dannemanna at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 14
You're still being too vague and still worrying about things that are not important. Since you're not really paying attention to what I'm saying (and what others are saying), I'm done here.Good luck.
jverda at 2007-7-12 22:16:21 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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

Navy_Codera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 16

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

Dannemanna at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 17
Riiiiiigggghhhtttt ... NOW I understand. (?)
Navy_Codera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 18

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

jverda at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 19
u want want a paint?]are u homer simpson?
Dannemanna at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 20
> u want want a paint?]> > are u homer simpson?? http://www.learnenglish.org.uk/
Navy_Codera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 21
whats your doubt in my question? kkk
Dannemanna at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 22
Potato bubble gum CAT! CATTTT!!!! Dog replies 30 chicken light salad soup preview monkey button. Any?
Navy_Codera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 23
... yeaaaahhh, I'm gonna get in trouble for that one...
Navy_Codera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 24
uauahuha:)
Dannemanna at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 25

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

jverda at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 26
> Potato bubble gum CAT! CATTTT!!!! Dog replies 30> chicken light salad soup preview monkey button. Any?You bad man monkeyist!
floundera at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 27
does anyone else smell a trll? As if this entire thread has been nothing but a set up?
petes1234a at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 28
List test = new ArrayList();This is the most efficient way because it is the shortest.
CaptainMorgan08a at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 29
> List test = new ArrayList();> > This is the most efficient way because it is the> shortest.LolList t = new ArrayList();
macrules2a at 2007-7-21 22:59:18 > top of Java-index,Java Essentials,Java Programming...
# 30
> NO> the MOST EFFICIENT WAY> >Collection test = new HashSet():Nonsense. What has efficiency got to do with it, anyway?
georgemca at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 31
Its not the case of efficiency only its about the use of interfaces.
Nistelrooya at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 32

> 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

georgemca at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 33
Instead of ArrayList test= new ArrayList();orList test = new ArrayList();or ArrayList<TYPE> test = new ArrayList<TYPE>();How about this:List<TYPE> test = new ArrayList<TYPE>();
George123a at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 34

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

georgemca at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 35
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.
George123a at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...
# 36

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

georgemca at 2007-7-21 22:59:24 > top of Java-index,Java Essentials,Java Programming...