Generic List in Java
Jakob Jenkov |
Java's List
interface (java.util.List
) can be generified. In other words,
instances of List
can be given a type, so only instances of that type can
be inserted and read from that List
. Here is an example:
List<String> list = new ArrayList<String>;
This list is now targeted at only String
instances, meaning only
String
instances can be put into this list. If you try to put
something else into this List
, the compiler will complain.
The generic type checks only exists at compile time. At runtime it is possible to tweak your code so that a String List has other objects that String's inserted. This is a bad idea, though.
Accessing a Generic List
You can get and insert the elements of a generic List
like this:
List<String> list = new ArrayList<String>; String string1 = "a string"; list.add(string1); String string2 = list.get(0);
Notice how it is not necessary to cast the object obtained from the List.get()
method call, as is normally necessary. The compiler knows that this List
can
only contain String
instances, so casts are not necessary.
Iterating a Generic List
You can iterate a generic List
using an iterator, like this:
List<String> list = new ArrayList<String>; Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ String aString = iterator.next(); }
Notice how it is not necessary to cast the object returned from the
iterator.next()
next call. Because the List
is generified (has a type), the compiler knows that it contains String
instances. Therefore it is not necessary to cast the objects obtained from
it, even if it comes from its Iterator
.
You can also use the new for-loop, like this:
List<String> list = new ArrayList<String>; for(String aString : list) { System.out.println(aString); }
Notice how a String
variable is declared inside the parantheses of
the for-loop. For each iteration (each element in the List
) this
variable contains the current element (current String).
Tweet | |
Jakob Jenkov |