wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes core java collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "core java collection" Watch "core java collection" New topic
Author

core java collection

vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
if there are plenty of records to iterate,which collection is best to use to iterate ? like array list ,queue..and why?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Each is different (and in some cases common).

Some depend on the ordering, or collating.. for example sorting a Collection of People by Age,
others simple such as sorting by insertion data or timestamp.

Some are key,value bases, some are not.

Some allow insertions/deletions before/after other elements in the Collection, maintaining ordering.

Hint: Try to use real world examples, for instance if you wanted to sort a Collection of your relatives by Age, how would you design that?

Why I interview candidates, I use a deck of cards (standard 52 cards, 4 suits, 13 values, no jokers) and ask them which they would use.

Pat.
Sunil Sunny
Ranch Hand

Joined: Oct 18, 2011
Posts: 41
As you are just a beginner you can use array list and use the for loop to iterate as Array List maintains indexes with no overhead. And also for loop works faster than iterator.

But as william said different collections for different purpose
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sunil Sunny wrote:As you are just a beginner you can use array list and use the for loop to iterate as Array List maintains indexes with no overhead. And also for loop works faster than iterator.


No, no, a thousand times no.

When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.

Please don't encourage these kinds of pointless microoptimizatoins.

(Also, note that the choice isn't for loop vs. Iterator. It's get(i) vs. Iterator. A very common idiom (one I used all the time before foreach came along) uses a for loop with an Iterator instead of a while loop.)
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Jeff Verdegan wrote:When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.

It could easily be the case that we are iterating with i because we need to know the value of i.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dennis Deems wrote:
Jeff Verdegan wrote:When iterating a collection, always use an Iterator or foreach loop (which is itself just syntactic sugar for an Iterator). Do not iterate using get(i). If get(i) is any faster, it will be by a tiny amount that will never be noticed in the context of an actual application, and it's a bad habit to get into, since, unlike Iterator/foreach, it's only viable for one particular collection and not something that can be used in the general case.

It could easily be the case that we are iterating with i because we need to know the value of i.


Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Jeff Verdegan wrote:
Dennis Deems wrote:It could easily be the case that we are iterating with i because we need to know the value of i.


Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.

"Total hack"?? Really, I think this is an extreme position.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dennis Deems wrote:
Jeff Verdegan wrote:
Dennis Deems wrote:It could easily be the case that we are iterating with i because we need to know the value of i.


Then add a counter variable. There's never a good reason for iterating with get(i), IMHO. It's a total hack.

"Total hack"?? Really, I think this is an extreme position.


To each his own I guess.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Using get(i) works nicely if you need to iterate only part of a List. Obviously it is best to restrict its use to Lists implementing RandomAccess.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

In other cases you should use subList.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: core java collection
 
Similar Threads
problem in encoding in the jsp page.
Extracting Database to put information in to creation of form
multi firing missles
Lookup key in HashTable
Help with java paint