It is possible, as both types implement Collection<E>
. The convention is for class types in Java to start with a capital letter. Since 1.5, Java has used generics for its collections and you should use them in all new code. Since you're using the 1.5 style for
loop, you should write generic code.
It's usually better to make functions operate on the least specific type as possible, as this gives the widest reuse. In this case, you only need something wich could go one up from Collection<T>
to Iterable<T>
, as you only need something which will work with the for loop. So combining generics and least power gives:
public class PrintLinePrinter {
public <T> void print (Iterable<T> collection) {
for (T item : collection) {
System.out.println(item);
}
}
}