Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
jeya lakshmi wrote:Then what is the difference between linkedlist and arraylist
If you frequently add elements to the beginning of the List or iterate over the List to delete elements from its interior, you should consider using LinkedList. These operations require constant-time in a LinkedList and linear-time in an ArrayList. But you pay a big price in performance. Positional access requires linear-time in a LinkedList and constant-time in an ArrayList. Furthermore, the constant factor for LinkedList is much worse. If you think you want to use a LinkedList, measure the performance of your application with both LinkedList and ArrayList before making your choice; ArrayList is usually faster.
Thomas Paul wrote:Check this article from the javaRanch newsletter:
http://www.javaranch.com/newsletter/June2002/newsletterjune2002.jsp#collections
Sam Samson wrote:I guess I should use LinkedList instead of ArrayList when I want to iterate over it and insert elements.
LinkedList is only slower when getting an element?
I can't remember that I've ever used get(index) on an ArrayList.
Almost everyone is saying that ArrayList should be chosen.
I don't understand the issue with "RandomAccess". What is exactly RandomAccess on an ordered List?
In a for-each loop, which method is called in behind for iterating? get(index)?
Stephan van Hulst wrote:Yes, ArrayList is generally faster. You should almost always prefer it, unless you are doing a *lot* of inserting and removing at places other than the end of the list.
Sam Samson wrote:
Thomas Paul wrote:Check this article from the javaRanch newsletter:
http://www.javaranch.com/newsletter/June2002/newsletterjune2002.jsp#collections
In this newsletter is a table. Regarding this table I guess I should use LinkedList instead of ArrayList when I want to iterate over it and insert elements.
I disagree. For the common use case--a bunch of add()s to build it, and then iterating using Iterator or foreach--they're both O(1) for the add and O(n) for the iteration.
Sam Samson wrote:Can you explain me that kind of notation to me? O(1) for LL vs. O(n) for AL. What's the meaning of O?
Stephan van Hulst wrote:
I disagree. For the common use case--a bunch of add()s to build it, and then iterating using Iterator or foreach--they're both O(1) for the add and O(n) for the iteration.
I meant faster in absolute terms, not in complexity class.
On my system, adding 10.000.000 objects to an ArrayList takes 300-500 ms, versus 2000-4000 ms for a LinkedList.
It seems it takes LinkedList a lot of time to create the node objects that wrap the elements.
Stephan van Hulst wrote:
I disagree. For the common use case--a bunch of add()s to build it, and then iterating using Iterator or foreach--they're both O(1) for the add and O(n) for the iteration.
I meant faster in absolute terms, not in complexity class. On my system, adding 10.000.000 objects to an ArrayList takes 300-500 ms, versus 2000-4000 ms for a LinkedList. If I set the initial capacity for ArrayList to 10.000.000, it takes 50 ms.
Stephan van Hulst wrote:I'm curious, could you run the code I posted and report what timings you get? Maybe play around with it, if you want?
I know there are factors involved that my test can't account for, but I still wonder![]()
[edit]
Hah wow you beat me to my own request!
Jeff Verdegan wrote:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan wrote:[...] I'm on 1.6 so can't use 10_000_000 [...]
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst wrote:My point is that unless you need random access, or random insertion/removal, there's no good reason to prefer one over the other, other for the fact that ArrayList generally has better coefficients. I think that's a good reason to use it as the default.
I agree it doesn't matter much in the face of bigger performance issues in an application, but what the hey.
Stephan van Hulst wrote:Yes, you'll always know ahead of time that you need random access, namely when you find yourself using the get() method in some iterative or recursive loop. When you use the get() method in response to some GUI event then you hardly need random access, since the user isn't going to notice any difference.
Rob Spoor wrote:
Jeff Verdegan wrote:
If you use Java 7 you can prevent that unnecessary calculation and simply write 40_000_000. Of course it's only syntactic sugar; the compiler will already turn 40 * 1000 * 1000 into 40000000.
Stephan van Hulst wrote:Yes, you'll always know ahead of time that you need random access, namely when you find yourself using the get() method in some iterative or recursive loop.
I guess in theory a LinkedList would put more strain on the GC, but it's very hard to make any assumptions about stuff that the JVM does behind the scenes.
Jeff Verdegan wrote:Except that the creator of the List doesn't always know how it will be used.
Stephan van Hulst wrote:
Jeff Verdegan wrote:Except that the creator of the List doesn't always know how it will be used.
Ahh, but that's why methods that do require random access can check whether it's provided ;)
It's funny how this old thread still managed to generate all this interesting discussion
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
|