-8

I have a question what java List I should use when I want that result will be printed from that List the same way it was added to it?

Should I use ArrayList or LinkedList?

And could you tell me the same about java Sets and Maps?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Paulius
  • 9
  • 2

2 Answers2

2

If I got this right, you want a collection that preserves the insertion order. Lists always do that (no matter what implementation you choose).

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Yes, you got this right. So what collections should I use? – Paulius Nov 14 '11 at 16:14
  • @Paulius what collection to choose depends on what you want to achieve. Have a look [here](http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) – mprabhat Nov 14 '11 at 16:16
  • Read the Javadoc for: `LinkedHashSet`, `ArrayList`, `LinkedList` and decide for yourself. The decision will probably be based on some other requirement, as all the classes that I mentioned will preserve the order of the elements... – Costi Ciudatu Nov 14 '11 at 16:17
0

I think that similar questions were asked already few times. Check out this: When to use LinkedList over ArrayList?

about other things like Map and Set you should find a Java book or tutorial and read a bit about them.

Community
  • 1
  • 1
gregory561
  • 14,866
  • 2
  • 23
  • 25