-4

Consider a method that returns a Collection or a method that needs a Collection as one of its parameters. Which is the simplest concrete type I can/should use?

  • ArrayList
  • HashSet
  • (something else)

I know that some will say "it depends", but what I want is a hard and fast rule of thumb that doesn't require you to do much thinking/analysis.

Roland
  • 7,525
  • 13
  • 61
  • 124
  • 4
    It depends and there isn't. – Sotirios Delimanolis Aug 08 '14 at 14:32
  • @SotiriosDelimanolis fine, what do you do when faced with the decision and you don't have time/energy to analyse? – Roland Aug 08 '14 at 14:33
  • 2
    It really depends on your requirements... you should be able to decide very quickly. – user1071777 Aug 08 '14 at 14:34
  • are the elements unique, do you have to retreive by index, is the collection legnth known - basic stuff – NimChimpsky Aug 08 '14 at 14:35
  • 1
    Different data types exist to address different problems. They have to be chosen based on what you need them for. You don't use a `List` if you need a key/value pair, you use a `Map`. And you don't use a `Map` if all you need is a list of objects. You need to think and find out which is the best data type for your requirement. – BackSlash Aug 08 '14 at 14:36
  • read documentation for `ArrayList` and `HashSet`, this will clear your doubt. – Vishrant Aug 08 '14 at 14:36
  • list and set have very different use cases. They are rarely interchangable. This question has not realy sense. – holap Aug 08 '14 at 15:05

1 Answers1

2

ArrayList : when order of element are important (Also may have duplicate values)

HashSet : when order of elements are not important and no need of having duplicates.

Something else: every Collection has its use/advantages and disadvantages.

if you really want to find the simplest one, then learn about each collections and based on advantages and disadvantages find the one suitable for you rather quickly.

nafas
  • 5,283
  • 3
  • 29
  • 57