collections in general

 

■ Top level collection tools are List, Set, and Map.

 

■ Collections have two big differences from arrays:

 

1 - Collections wonít hold primitives (they hold objects only)

2 - Most collections grow or shrink automatically for you

 

■ All collections reside in java.util.Don't forget you need an import statement whenever you use one.

 

■ Operations on collections never require mandatory try-catch blocks. However, most collection methods can throw exceptions, so you will probably want to use try-catch blocks anyway.

 

■ Older pre-1.2 Java classes such as Vector, Stack and Enumeration are still commonly used despite the fact that suggested "replacements" exist.Those classes are thus included here.

 

■ Some collections allow duplicate entries and some don't. Some allow null entries and some donít, and so on.

Here's a handy reference table of the more common collections.

 

 

No. of
logical
columns

Allows
Duplicate
Entries

Allows
Null
Entries

Allows
Primitives

as entries

Provides

automatic

ordering

Ordering

is pre-

dictable

To

traverse it,

use

Lists:ArrayList

1

Yes

Yes

No

No

Yes

Iterator

††††††††††† LinkedList

1

Yes

Yes

No

Yes

Yes

ListIterator

†††††††† †††Vector

1

Yes

Yes

No

No

Yes

Enumeration

††††††††††† Stack

1

Yes

Yes

No

No

Yes

Enumeration

Sets:†† HashSet

1

No

 

No

No

No

 

†††††† †††††TreeSet

1

No

 

No

Yes

Yes

 

Maps: HashMap

2

Not keys

 

No

No

No

 

†††††† †††††TreeMap

2

Not keys

 

No

Yes

Yes

 

 

††† Sun has a nice explanation of all collections, including an API, and a more complete list of all the various collection classes at http://java.sun.com/j2se/1.4.2/docs/guide/collections/index.html