This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 8 years ago.
What is the difference between ArrayList and LinkedList? I watched a video on it by TheNewBoston but i am still confused. Please answer in a short form and in plain English. Please do not use any advanced code.
ArrayList is a list implementation that's backed by an Object[]. It supports random access and dynamic resizing.
LinkedList is a list implementation that uses references to head and tail to navigate it. It has no random access capabilities, but it too supports dynamic resizing.
Bear in mind that both support the get(int index) signature, but the difference between the two implementations is performance: with an ArrayList, that's a matter of going to the index position, whereas with a LinkedList, you have to walk down the object chain (either from the front or the rear, depending on what you've indexed into).
For an arrayList, you have access to every element, which has its own index value. For example, if you want the third item in an ArrayList, you just have to perform arrList.get(2) to get this value. An ArrayList is made using a similar structure as an array.
For a linked list, you ONLY have access to the first element, but each element has access to the next one. So, to get to the third element, you have to go to the first, then second, then finally third. Think of a LinkedList like a chain. If you have the first part of the chain but cut off its access to the second part, you lose the rest of it too.
They both have their advantages and disadvantages, in terms of memory, processing time, and ease of use. Let me know if you have any more specific questions or want clarification.
Related
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Which Java Collection should I use?
(6 answers)
Closed 6 years ago.
I want to know when to use Set and List.On which basis it should be decided.For example, when we are dealing with order application, at that what should I use?
List any = new ArrayList<>();
or
Set any = new HashSet();
or LinkedList.
It all depends upon your current requirements
for example, consider some important points about
If you want to access the elements in the same way you're inserting them, then you should use List because List is an ordered collection of elements. You can access them using get(int index) method, whereas no such method is available for Set. The order in which they will be stored is not guaranteed.
If your elements contains duplicates, then use List because Set doesn't allow duplicates whereas if your elements are unique, then you can use Set.
As far as LinkedList and ArrayList are considered:
LinkedList are slow because they allow only sequential access. But they're good if your elements size is regularly changing, whereas if the size of your elements is fixed, then you should use ArrayList because they allow fast random read access, so you can grab any element in constant time.
However, ArrayList are not good when you require large delete operations because adding or removing from anywhere but the end requires shifting all the latter elements over.
ArrayList are not considered good when you have to insert anything in middle because if you want to insert a new element in the middle (and keep all the elements in the same order) then you're going to have to shift everything after that spot where element was inserted, whereas such operation in LinkedList requires only change of some references.
Take a look at the features of several data structures and according to your requirements, you can decide where you should use which data structure.
also see:
- When to use LinkedList over ArrayList?
- What is the difference between Set and List?
- What Java Collection should I use?
- Insertion in the middle of ArrayList vs LinkedList
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 7 years ago.
I have to sort a large list (more than 10,000 elements). On adding an element I have to insert it on the right place. I saw that an ArrayList will shift all the element that are after the insertion point.
How do all the different implementation of the List interface behave in such a case? And what are the pros and the cons when choosing one implementation over the other?
The two main implementations of List are ArrayList and LinkedList. There are others but they are generally used in specialis situations.
ArrayList can be accessed very quickly by index because it is backed by an array - you just need array[i] - but modifying the list requires moving much of the underlying array around so that is not efficient.
You can add/remove items with LinkedList very efficiently but finding the nth entry is slow because it has to start at the head and walk the list counting nodes until it gets to the required location.
Probably duplicate, but i will give you a hint.
To sort your data you can use:
Collections.sort(List list);
method, it will convert List to Array anyway, so you don't have to care about too much about type of List implementation. All it needs is interface Comparable implemented in your objects.
Can your data contain duplicates?
If NO, you can use a TreeSet<?>
If YES, you can use a TreeMap<?, Integer> where the Integer is the count per item
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between ArrayList and Vector?
I've been using Vectors quite a lot in my recent program. However I've read somewhere that Vectors are a bit old. Whether that means it will be obsolete or phased out of Java is debatable. So the recommendation was that ArrayLists should be used instead. I've noticed that ArrayLists don't have a method remove(int index, Object object) while vectors do. The reason I ask is suppose I add a string, say "String 1". And I attempt to add the same string again. How do I remove the first string without counting the occurrences of it in an array list.
However I've read somewhere that Vectors are a bit old : Yes vector class is considerd as legecy and is still in library to support old applications. It is replaced by Collections.synchronizedList(list) .
I've noticed that ArrayLists don't have a method remove : You can remove the data based on Index. if u want to remove the object use boolean java.util.ArrayList.remove(Object o) : dont forget to override the equals and hashcode method :)
How do I remove the first string without counting the occurrences of it in an array list : Best thing is to use set. If thread safty is a concern use <Object> Set<Object> java.util.Collections.synchronizedSet(Set<Object> s)
Hope all your questions are clarified.
Regards,
Punith
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a List vs. an ArrayList?
Ive used both of them, but im just wondering what are the pros and cons between them? What are the major differences? And which one is better to use?
Thanks.
List is an interface implemented by ArrayList class. Another well-known implementation of the List is LinkedList.
ArrayList provides constant-time random access, while LinkedList provides constant time for non-sequential access. When you declare a variable that will hold an ArrayList, consider accessing it through an interface, like this:
List<ElementType> myList = new ArrayList<ElementType>();
This will let you swap in a different implementation without disturbing the rest of your code.
List merely describes the contract of what it means to be a list. As such, it is not a concrete implementation but merely an interface. A list can be implemented in a number of ways.
In particular, you have ArrayList, which internally keeps a dynamic array for storing all the elements in order. You also have LinkedList, which stores elements as a doubly linked list i.e. a sequence of nodes which keep references to the previous and next nodes.
Vector is another List, much like an ArrayList in that its implementation is based on a dynamic array; it's, however, a relic of the older versions of Java and is guaranteed to be thread-safe by being wholly synchronized. In practice, new Vector<T>() is more-or-less equivalent to Collections.synchronizedList(new ArrayList<T>()).
The reason for having a List is that a list can come implemented in a number of ways. That being said, often you want to have some sort of generic behavior that can be applicable to all Lists... see polymorphism.
A List is an interface, and an ArrayList is an implementation of that interface. An ArrayList is a List, and so are LinkedLists, Stacks, Vectors, etc.
the other posters already answered the "what" part of your question. Some considerations to think about when choosing between them.
An ArrayList uses an array behind the scenes. So accessing by index can be done in constant time. Adding can also be done in constant time, if the array has been allocated with enough space. However, when the space runs out, ArrayList will allocate a larger array and copy the old array values into the new one.
A LinkedList uses nodes that are chained together. Accessing by an index can potentially require walking the entire list (linear time). Inserting only requires creating a new node and adding it at the end (which could be constant time if a tail pointer is maintained).
So "which one is better" can depend on how you are using it. Truthfully, I've never measured performance differences between the two, but it's just something to consider.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to use LinkedList<> over ArrayList<>?
This is a genuine attempt to know when would one use a LinkedList;
From what i understand since the java.util.LinkedList doesn't support random access, the only way to get the nth element is to skip from 1 to (n-1) or use get(n) which itself is very inefficient.
So why would one use a LinkedList? An ArrayList would serve for most part unless you want to iterate the collection from both sides using a ListIterator?
Think about this method:
List list = // choose your list here
list.add(0, new Object());
For large lists, LinkedList will heavily out-perform ArrayList. The same applies for
list.remove(0);
... and many other methods. For more information, I suggest reading about the java.util.Deque interface, which is also implemented by LinkedList
Consider 3 common operators on these sorts of data structures: random element access, adding elements, and removing elements.
In LinkedList, your random element access is slow (O(N)), but adding and deleting are fast (O(1). For ArrayList, the reverse is true: random element access is fast (O(N)), but adding and removing elements are slower.
You need to look at which operations your system will do more of and use the appropriate data structure.
LinkedList better suited (as compared to ArrayList) for insertions/deletion for arbitrary indexes. When inserting or deleting from an ArrayList, the internal array has to be shifted. For LinkedList, it's a matter of simply repointing the the nodes' pointers.