I'm trying to build a container type called ListNamed, which is essentially a list, but one that allows element lookups by name, where multiple elements can have the same name, and in which case the lookup would return the first element found with a matching name (that's why I can't use a HashMap -> order matters, and keys are not-unique).
My initial version, which works:
public class ListNamed<T extends Named> extends ArrayList<T>{
public T get(String name){
for (T x: this){
if (x.getName().equals(name)){
return x;
}
}
return null;
}
}
However, there are cases where I would like to specify a different implementation of List - Instead of ArrayList, there are cases where I would like to use LinkedList.
So, is there a way to specify the extended implementation via generics, or will I have to create separate classes (ArrayListNamed, LinkedListNamed)?