aspose file tools*
The moose likes Beginning Java and the fly likes Collections: When to use which (Set, Array, List) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections: When to use which (Set, Array, List)" Watch "Collections: When to use which (Set, Array, List)" New topic
Author

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

Mallik kannan
Greenhorn

Joined: Sep 07, 2009
Posts: 4
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Feb 26, 2001
Posts: 4462
    
    6

Search for java collections tutorials - you should find the answers there


Junilu - [How to Ask Questions] [How to Answer Questions]
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
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

Joined: Feb 09, 2012
Posts: 583

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.


Tell the difficulties that i am difficult.
BalaMurali dhar
Ranch Hand

Joined: Apr 14, 2012
Posts: 60
Array : For Fixed Length
set : It cannot contain duplicate elements
list : It is permitting duplicates
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections: When to use which (Set, Array, List)