Difference between arraylist and linkedList [duplicate] - java

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.

Related

How to decide which collection should be used from List Set? [duplicate]

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

List insertion efficency [duplicate]

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

What collection should I use [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Rule of thumb for choosing an implementation of a Java Collection?
My situation is this:
I have a collection of objects that I need to hold and every now and then iterate through
The size of the collection is dynamic
The iteration should access each element
The collection does not need to be sorted
Creating or updating the collection has no time constraints but I'd like to iterate through the collection as fast as possible.
What would be the best Collection to use (or would you perhaps suggest using an array?)
You can use a List collection probaly ArrayList.
It depends on the parameters like whether it is an ordered collection, whether you want to maintain the insertion order, whether you want to maintain uniqueness etc
List vs Set
Set: Unique, unordered collection
List: ordered collection, allows duplicate elements
ArrayList vs LinkedList
In general : If you don't have particular constraints, an ArrayList is you best bet. Unless you have extremely tight performance control, don't go for a blank array, you have too much chance for errors (and a big chance of not outperforming the ArrayList implementation).
In your case the fast iteration requirements means ArrayList is a good choice.

java - what is the difference between a list and an arraylist [duplicate]

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.

When would you use a java.util.LinkedList [duplicate]

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.

Categories