Java Collection Objects in the java.util package
Archive - Originally posted on "The Horse's Mouth" - 2009-08-05 10:49:02 - Graham EllisIf 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 and Hashtable.
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.