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.
Related
I appeared for an interview where interviewer asked me about ArrayList, Linked list and Vector. His question was
ArrayList, LinkedList, and Vector are all implementations of the List interface. Which of them is most efficient for adding and removing elements from the list ? And I was supposed to answer including any other alternatives I may be aware of.
I answered him but he seems little not impressed by my answer.
Can someone tell me more about this ?
Thank you
LinkedList is implemented as a double linked list. It's performance on add and remove is better than Arraylist, but worse on get and set methods.You will have to traverse the list up to a certain point in those cases. So, definitely not LinkedList.
ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.
Vector is similar with ArrayList, but it is synchronised.
ArrayList is a better choice if your program is thread-safe. Vector and ArrayList require more space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time.
LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.
A lot is dependent on what kind of requirement you are working on. A decision can be taken depending upon needs.
LinkedList is best suited for adding/removing items, reason being you just change the links between the items without manipulating other unrelated items to accomplish current operation. This also makes linked lists comparatively faster than other containers.
Cheers!
Choose LinkedList if you have a lot of data to adding and removing from list but be careful if you wish to get element from your list than this will be not right Data Structure.
List<T> list = new LinkedList<T>();
LinkedList is implemented using Double linked List,since its perrformance is good at adding/removing elements from/to list.
Performance of ArrayList vs. LinkedList :
The time complexity comparison is as follows:
get() : O(n)
add() : O(1)
remove() : O(1)
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 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<>?
When to use a linked list over an array/array list?
When should I use arrayList and when should I go for LinkedList?
When should I use TreeSet, LinkedHashSet and HashSet?
When should i use arrayList and when should I go for LinkedList?
Arraylist maintain indices like arrays. So if want more frequent get operations than put then arraylist is best to go.
LinkedList maintain pointers to elements. you can't to a specific index like in arraylist. But the advantage here in linkedlist is that they don't need to shift back and forth like in arraylist to maintain continues indices. So get operations in linkedlist are costly as you would have to go through pointers to reach your elements. But put operations are good as compared to arraylist. you just need to connect to pointers and that's it.
When should I use TreeSet, LinkedHashSet and HashSet?
the difference is only in ordering. treeset elements need to maintain a specific orders defined by your member objects.