How do I decide which data structure to use for a more efficient program? I mostly use ArrayList. I checked out other questions but I was not fully satisfied. If someone could help me I would be really happy.
Asked
Active
Viewed 2,290 times
0
-
Before posting this question, did you check and search in internet ? – Sambit May 25 '19 at 10:02
-
You can easily get it from internet. Please follow https://www.connect2java.com/tutorials/collections/arraylistlinkedlistvector-and-stack/. If you have any specific doubt then please post it. – Mani May 25 '19 at 10:14
-
They all differ in the specific implementation, on how they keep track of the order and searchability and accessability. So each comes with pros and cons. – Sunchezz May 25 '19 at 10:15
-
Thanks everyone for sparing your time. connect2java.com/tutorials/collections/…. This link helped a lot. – Can Kırşallıoba May 25 '19 at 10:47
-
Ruslan I looked at that question but I was also wondering about when to use stacks. But you are right it resembles that question a lot. I will try to search for existing questions better next time. – Can Kırşallıoba May 25 '19 at 10:49
-
Use a stack when you need a last-in-first-out (LIFO) queue. Note, you should use an implementation of [`Deque`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/Deque.html) (such as `ArrayDeque`) rather than a `Stack` in modern versions of Java (and by modern I mean Java 1.6+). – Slaw May 25 '19 at 19:21
1 Answers
1
As ArrayList and LinkedList both implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations. 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. LinkedList is implemented as a double linked list. Its performance on add and remove is better than ArrayList, but worse on get and set methods. So basically when you working with data that needs to be frequently added or removed from the list you would like to go for the LinkedList.

makota
- 26
- 4
-
-
2@CanKırşallıoba Never. As `Stack` states in its own documentation, it is super old and got replaced by implementations of the `Deque` interface. Prefer those instead, like `ArrayDeque` for example. – Zabuzard May 26 '19 at 08:52
-
@Zabuza Thank you very much, will look in the documentation in a more detailed way next time. – Can Kırşallıoba May 26 '19 at 10:25