Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections: When to use which (Set, Array, List)

 
Mallik kannan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
In most of the interviews, im facing this question, but if i explain the Concept of Set, Array, List. Almost everytime, Interviewer is not satisfied,and they are concentrating on how to know "Which one to Use When"?
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an Array only when you know the size of the collection will never change, and when the ease of access by array index trumps every other consideration. Almost everything you need to do is easier when working with a List or a Set.
Use a Set when you want to be sure that the collection will contain no duplicate elements.
Use a List if your collection needs to accommodate the possibility that elements appear more than once. The List should be your go-to data structure; use it unless you have a good reason to use something else.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for java collections tutorials - you should find the answers there
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mallik kannan wrote:Which one to Use When"?


Set : For unique data.
Array: For fixed length and fixed type data.
List: For duplicate data. (require to save;needed to save...)
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:
Mallik kannan wrote:Which one to Use When"?


Set : For unique data.
Array: For fixed length and fixed type data.
List: For duplicate data. (require to save;needed to save...)


Array is not part of Collection However ArrayList is the part of Collection and that has dynamic length.
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array : For Fixed Length
set : It cannot contain duplicate elements
list : It is permitting duplicates
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mallik kannan wrote:but if i explain the Concept of Set, Array, List. Almost everytime, Interviewer is not satisfied...

Probably because you don't know what they are yourself; and the only way to find out is (as others have said) to read either (a) the tutorials, (b) the API documentation, or (c) (if you can afford it) a good book.
Personally, I'd go with (b) because not only do they tell you what they are meant to do (look at the interface docs), but also precisely how they are implemented for Java. Some people find them too dry though, so I can quite understand why you might want to go with a tutorial.

I'm surprised that nobody's mentioned Maps...

Wisnton
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic