17

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?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
constantlearner
  • 5,157
  • 7
  • 42
  • 64
  • 3
    Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Jul 26 '12 at 11:09
  • 1
    Also see [Hashset vs Treeset](http://stackoverflow.com/questions/1463284/hashset-vs-treeset) and [HashSet vs LinkedHashSet](http://stackoverflow.com/questions/5080612/hashset-vs-linkedhashset) regarding your second question. – Joachim Sauer Jul 26 '12 at 11:10

1 Answers1

18
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.

Ahmad
  • 2,110
  • 5
  • 26
  • 36