0

I see that there are a ton of generic data structures provided in Java. They all implement List, so they can be used almost interchangeably, but when would I want to use each? Personally, I stick to LinkedList because it's something I'm "familiar" with. I'm not asking for an explanation of every single structure, but can you explain some of the more common ones and give their uses, as well as compare and contrast the uses of "Vector-like" structures?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Simon Kuang
  • 3,870
  • 4
  • 27
  • 53
  • 1
    Huh? A lot of `Collection`s don't implement `List`. There *are* quite a few `List` implementation, but also quite a few collections that aren't lists. –  Mar 03 '14 at 16:41
  • 1
    This is a huge question. Simple answer, always use the most appropriate data structure for the problem you're solving. Start reading :) – William Morrison Mar 03 '14 at 16:41
  • Use vector class if synchronization is require.Use ArrayList or linkedlist depend on the requirement because having different complexity to search,add or delete element – Kick Mar 03 '14 at 16:41
  • Don't use `Vector` in new code. Otherwise, use `ArrayList` if you need random access, or `LinkedList` if you need lots of inserts and removals, especially from the middle. This is basically a classic CS data-structures issue. – chrylis -cautiouslyoptimistic- Mar 03 '14 at 16:42

2 Answers2

0

It depends on the performance characteristics and behavior you are looking for.

For example in a LinkedList add, delete, and retrieve are O(1), O(1), and O(n), whereas for an ArrayList, the same operations are O(n), O(n), and O(1) if using get(int) and O(n) if using get(Object). However ArrayList uses less memory than LinkedList per entry.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • retrieve on array list is O(1) isn't it? – William Morrison Mar 03 '14 at 16:43
  • @WilliamMorrison No, it still has to iterate over each index and perform `.equals`. – Vivin Paliath Mar 03 '14 at 16:43
  • I mean for `get(int index)`. I see what you mean now though. – William Morrison Mar 03 '14 at 16:44
  • While all this could be technically true with certain definitions of "add", "delete" and "retrieve", this is still a wholly misleading and inadequate characterization of the differences. -1 –  Mar 03 '14 at 16:44
  • @WilliamMorrison Oops, yes if you are using `get(int)` – Vivin Paliath Mar 03 '14 at 16:44
  • 1
    @delnan I wasn't aiming to write a scholarly explanation of the various differences between linked list and array list, which is why I gave a single example. – Vivin Paliath Mar 03 '14 at 16:45
  • There are many very simplified, non-"scholarly" explanations of arrays vs. linked list, some of them I endorse and consider useful, but this is one is not among them: It highlights irrelevant operations (`get(Object)`, add/delete seem to assume you already have an iterator) and ignores other important properties (cache behavior, efficient *appending* to an `ArrayList`), and does not give any guidance. In short, I think it doesn't really help a newbie in deciding what to use. Even an absolute "always use `ArrayList`" statement would be more useful. –  Mar 03 '14 at 16:49
  • @delnan Retrieval is not an "irrelevant" operation. I mentioned `get(Object)` simply in response to a comment. The question itself is overly broad so there is simple answer. If you feel so strongly about it perhaps you should post your insightful reply and let us all benefit from it. – Vivin Paliath Mar 03 '14 at 17:01
-2

One often uses Vector<type> to add elements to the structure that are part of the same collection, but do not have any relationship to other members (other than being part of the same collection). A LinkedList indicates that there is some sort of ordering that is important among the members of the collection.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
  • 2
    -1 -> Usually one should not use vector for it's synchronized and should prefer Array/Linked list. And actually everything you wrote is bullsh*t :) – Svetlin Zarev Mar 03 '14 at 16:55
  • Performance questions can come later .. one does want thread-safe and easy to use implementations. Vector does have its place. The question was about why one might use one construct versus another. This was an example about why a Vector might be used over a LinkedList. There are additional programming considerations based on how the data structure must be used for a particular problem being solved. – ErstwhileIII Mar 03 '14 at 16:59
  • I disagree: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Svetlin Zarev Mar 03 '14 at 17:00
  • We have begun discussing "best uses" of data structures, but the question was about why not use a single data structure all the time. Performance considerations concerning Vector are important for many questions, just not this one where Vector was used as an example data structure versue LinkedList because it demonstrates how structures are different (before we get into performance considerations). – ErstwhileIII Mar 03 '14 at 17:23
  • If you've read what I've posted, then you should have understood, that `Vector` is broken by design and one should not use it. Either way, I didn't downvote your question because of the vector, but because everything you've said is plain wrong. – Svetlin Zarev Mar 03 '14 at 17:27