If you want to hold a number of objects in a single composite object, you call it a collection. The Java language itself (without any additional classes) supports arrays, which can hold primitives or objects (example
). The java.util package adds a whole further series of classes which can be used to hold multiple objects in various arrangements and with various facilities.
Do note one important limitation - they hold multiple OBJECTS; if you want to hold multiple primitives in a collection, you need to use the data type wrappers - you can find an example that compares a float primitive to a Float object here
(and note the different capitalisation - float v Float)
The first collection objects in Java were the Vector, Stack and Hashtable, using the enumeration interface. They have been available from the beginnings of Java. A complete Collection framework was added at Java 1.2, with objects such as ArrayLists, HashSets and HashMaps.
I am noticing a considerable number of visitors arriving at our web site looking for examples of these various classes, so the purpose of this short item is to help put them into context, and provide associated links.
a) The OLDER collection objects - Vector, Stack and Hashtable, are slower in operation but remain a part of the language (not deprecated) because they are Thread Safe. Newer collection objects - ArrayLists, HashSets and HashMaps - run faster but may give you coding issues when you're running threaded code.
b) Vector, Stack and ArrayList are indexed (like an array) from position 0 upwards. Unlike an array, each of them can be extended after it is created, and they can be sorted, have elements added in and taken out from the middle efficiently, and so on. In contrast, Hashtables, HashSets and HashMaps are keyed to another object not an index number, using a "hashing technique". This makes it very efficient indeed to find whether an element already exists and to add new elements, but means that you can NEVER sort them into order. (Instead, you can make an ArrayList of the key names and sort that, using the sorted arraylist to give you a traversal order)
c) The Stack is a special case of a Vector - a subclass - with extra methods isEmpty, push and pop. Although the previous paragraph is correct in saying you can sort (etc) a stack, it would be a peculiar thing to do!
d) The HashSet is a series of keys WITHOUT values. There are times when you want to register merely the existence of a key, rather than assign a value to it - for example, you might want to analyse a web access log file to produce a report (list) of all visiting IP addresses. It turns out that placing each item into a HashSet is the most efficient way to do it, rather than the more obvious way of keeping a list and checking each new item against the list.
Source code examples for the six collections I have mentioned ... HashMap HashSet ArrayList Stack Vector
Our Learning to Program in Java course
(for newcomers to programming) and our Java Bootcamp course
(for delegates with prior programming experience) both teach you Java up to and including the level of the utility classes, setting you up with all the fundamentals of the Java language and associated basic extra classes that form a necessary part of most real life applications of the language. (written 2009-08-05, updated 2009-11-02)
Associated topics are indexed underJ714 - Java - Fundamental classes 
String handling - from first steps to practical examples - (2010-11-13) 
Sorting - naturally, or into a different order - (2010-08-14) 
for and foreach in Java - (2010-04-22) 
Length, size or capacity in Java? - (2010-02-24) 
Sorting Collections of Objects in Java - (2009-09-25) 
Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24) 
Java - Generics - (2008-11-27) 
Java, sorting, ArrayList example, generics - (2008-01-11) 
Java sorting - comparable v comparator - (2007-02-02) 
Do languages change? - (2004-09-08)
Some other Articles
Planning!Learn a new programming language this summer.Apache, Tomcat, mod_proxyWhat search terms FAIL to bring visitors to our site, when they should?Java Collection Objects in the java.util packageLooking for a practical standards courseUploading and Downloading files - changing names (Perl and PHP)Helping new arrivals find out about source code examplesGraphics in Lua - an example using the gd libraryFor Lua Programmers AND for Town Planners