Collections

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.

Vectors

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 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 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){

HashMap

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.

TreeSet

For example the TreeSet collection ensures that its elements are sorted. 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 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.

Last modified: Thursday, 24 July 2014, 2:54 PM