CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Collections

Collections and Generics
Java collections
Generics

An introduction to generics

  • Generics refers to a new feature of Java 5.0 that lets you create typed collections.
  • A typed collection can hold only objects of a certain type.
  • To declare a variable that refers to a typed collection, list the type in angle brackets (<>) following the name of the collection class.
  • When you use a constructor for a typed collection, you specify the type variable in angle brackets following the constructor name.
  • The type variable can't be a primitive type such as int or double. However, it can be a wrapper class or a user-defined class.
  • If you do not specify a type for a collection, the collection can hold any type of object. However, the Java compiler will issue warning messages whenever you access the collection to warn you that type checking can't be performed for the collection.
  • To create a generic class that lets you specify type information, specify one or more type variables in angle brackets following the class name on the class statement.
  • Then, you can use the type variable within the class anywhere you would normally specify a type.

The syntax for specifying the type of elements in a collection

CollectionClass<Type> collectionName = new CollectionClass<Type>();

The generic version of ArrayList

ArrayList<E>

	where E is a type parameter for some well-defined reference type
	such as String, Integer, or Book.

A statement that creates an array list of type String

ArrayList<String> codes = new ArrayList<String>();

A statement that creates an array list of integers

ArrayList<Integer> numbers = new ArrayList<Integer>();

Code that creates a linked list of type Product

LinkedList<Product> products;
products = new LinkedList<Product>();

The syntax for declaring a class that uses generic types

public class ClassName<TypeVariable [,TypeVariable]...>{}

A class statement for a class that implements a queue

public class GenericQueue<E>{}

Example

Prior to Java 5.0, here's how we use ArrayList. Suppose we want to maintain a list of Book objects in which the Book class is defined as followed:

class Book {
	private String author;
	public Book(String name) {
		setAuthor(name);
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
}

Here's a code that adds Book object to a list:

import java.util.*;
...
list bookList = new Arraylist();

bookList.add(new Book("Jane Austin"));
bookList.add(new Book("Charles Dickens"));
bookList.add(new Book("henry James"));
...

To display author information in bookList, we use an iterator

Iterator itr = bookList.iterator();
while (itr.hasNext()) {
	Book book = (Book) itr.next(); // typecast is required
	System.out.println(book.getAuthor());

Alhough we know bookList contains nothing but Book objects, typecasting is necessary here because an instance of any class can be added to the bookList. So the statement

bookList.add(new String("Java"));
ould execute fine. Of course the above iterator loop will fail if bookList contains an object other than Book. What we want here is a way to specify a homogenuous collection of objects; that is, every object in a collection is drawn from the same class. this improves the type safety.

Restrictions on Generics

Java places some restrictions on the uses of generics. Two of them are:

  • Java does not allow generic arrays. The statement
  • E[] myArray = new E[size]; // illegal
    attempts to create an array called myArray that holds elements of type E. This is illegal; instead, use an explicit cast, such as
    E[] myArray = (E[]))new Object[size]; // legal
    Be aware that the compiler will issue a warning because it doesn't know whether or not this type of cast is safe.

  • Java does not permit instantiation of a generic type. For example, the method
  • public illegalMethod (E t) { E copy = new E(); // illegal ....
    generates a compilation error.

Previous | Comparison of arrays and collections | Java collection framework | Collection classes | Generics | Homogeneous list | Next