Java Generic Collections
Jakob Jenkov |
It is possible to specify generic types for most (if not all) of the components in the Java Collections API.
In this tutorial I will explain how to specify a generic type for a couple of collection types. Seeing these
examples should give you an idea about how to use generics with the Java Collections API.
This text will not cover Java generics in general. Java Generics is
covered in my
Java Generics tutorial.
Generic Collection Example
When you set a generic type for a Java Collection, you do so when you declare the variable referencing it. Here is an example of setting the generic type of a Collection and HashSet to a Java String - meaning it can only contain String instances:
Collection<String> stringCollection = new HashSet<String>();
This stringCollection
can now only contain String
instances.
If you try to add anything else, or cast the elements in the collection to any other
type than String
, the compiler will complain.
Actually, it is possible to insert other objects than String objects, if you cheat a little (or is just plain stupid), but this is not recommended.
You can specify a generic type for a List's
, Set's
etc.
Generic Iterator
When you have specified a generic type for a Java collection, that generic type also works for the
Iterator returned by the iterator()
method. Here is an example of how obtaining an Iterator
with a generic type set on it looks:
Iterator<String> iterator = stringCollection.iterator();
You can iterate the elements of this Iterator like this:
while(iterator.hasNext()) { String element = iterator.next(); //do something with element. }
Notice how it is not necessary to cast the String returned from the iterator.next()
method call.
Because the Iterator has its generic type set to String, the Java compiler already knows that next()
will return a String.
Generic Iteration Using for Loop
You can also iterate the above collection using the new for-loop, like this:
Collection<String> stringCollection = new HashSet<String>(); for(String stringElement : stringCollection) { //do something with each stringElement }
Notice how it is possible to specify the type of the variable of each element (stringElement
)
as String. This is possible because the generic type of the Collection is set to String.
Tweet | |
Jakob Jenkov |