Previous |
Next |
Collections are a way of storing multiple objects in a single named container. Unlike arrays you do not need to know the size at the time of creation and arrays have their own characteristics and methods. Note that Collections can only store objects, not primitives. If you have need to store primitives you can use the wrapper classes which as the name implies, act as a wrapper class for primitive types. All of the collection classes implement the collection interface which supplies some very basic functionality to be implemented.
The different collection classes perform different purposes, for example, key/value pairings, sorted collections,ordered but not sorted and some choices to be made will affect performance. For the purpose of this book I will just cover some of the more commonly used collections and not get into the details of how you can optimise for performance.
The Vector class was part of Java from JDK 1.0 and you you can see the API documentation for the it class at
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html
The Vector collection class allow the storage of objects and unlike an array does not need to know how many will be stored when it is created. In many ways it is the collection class most analogous to an array in that the elements can be accessed using and index offset. Thus if you have an array called days with elements one, two and three, you can access element zero as
days[0]
With the equivalent vector you can access element zero as
days.get(0);
The following code shows how an instance of Vector can be created, populated with elements and then each element accessed in turn.
import java.util.*;
public class VecStore{
public static void main(String argv[]){
new VecStore();
}
VecStore(){
Vector<String> v = new Vector();
v.add("Xavier");
v.add("Bloggs");
v.add("Andrews");
v.add("Jones");
for(String s : v){
System.out.println(s);
}
}
}
The output from this program will be
XavierBloggsAndrewsJones
The program has simply stored the text and then output it in the same order it was entered.
Note the strange syntax on creating the Vector reference with the angle brackets <String> is an innovation bought in with JDK1.5 called Templates. This means that the TreeSet knows it is receiving Objects of type String. Without the use of Templates the type within the for loop would have to be Object as in
for(Object s :v){In computing terms a Hash is sometimes known as an associative array. It is a represents the relationship between a key and a value. Given the key you can look up its matching value. For example you might set up a hash using a student ID as the key and the student name as the value. The key must be unique (so it can identify the value). The following code shows how you can populate a HashMap with a number and a String and then use the number as the key to directly look up the string.
import java.util.*;
public class HashStore{
public static void main(String argv[]){
new HashStore();
}
HashStore(){
HashMap names = new HashMap();
names.put(99,"Xavier");
names.put(12, "Bloggs");
names.put(1, "Andrews");
System.out.print(names.get(1));
}
}
If you compile and run this code it will output Andrews as that is the value that maps to the key value 1.
According to the API documentation “This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.” This can be deceptive as you may find that when you retrieve elements they are returned in an expected order (e.g. The order they were added). However just because this happens five times, or ten times on your machine does not mean it is guaranteed. If you want to a collection that stores key/value pairs in a determined order then you need the TreeMap collection.
The TreeSet collection stores individual elements and ensures that its elements are sorted according to “natural order”. Without getting into too much detail, natural order is generally what you would expect, i.e. If it is letters it will be stored alphabetically, it it stores instances of the numeric wrappers it will be sorted numerically. So if you were storing a set of names you could use the TreeSet to ensure they are sorted. The following code illustrates this
import java.util.*;
public class NameSort{
public static void main(String argv[]){
new NameSort();
}
NameSort(){
TreeSet<String> names = new TreeSet();
names.add("Xavier");
names.add("Bloggs");
names.add("Andrews");
names.add("Jones");
for(String s : names){
System.out.println(s);
}
}
}The output from this program will be AndrewsBloggsJonesXavier as the TreeSet collection stores its elements in “natural order”, which in the case of Strings will be alphabetical. Note that the fact that this class is called TreeSet tells you it implements the Set interface. The term Set in this context means that each element must be unique. If you add the same element twice TreeSet will not throw an exception but you will silently “throw away” the duplicated element. You can check for this condition by inspecting the value returned when the add method is called.
Other resources
Collections in the Sun tutorial
http://java.sun.com/docs/books/tutorial/collections/
Previous |
Next |