You have mixed Generic with raw type. It will compile fine but at run-time it might fail because Generic information is lost at run-time.
Generic should be used to track such bugs at compile-time.
It's better explained at What is a raw type and why shouldn't we use it? in detail.
Warning: Type safety: The method genericFunction(List)
belongs to the raw type GenericClass
. References to generic type GenericClass<E>
should be
parameterized.
If you have two methods with same name with different Generic type of List then it results into compile time error. The compiler is unable to resolve Generic type in case of method arguments that can be proved by below sample code.
Sample code: (compiler error - not a valid overloaded method)
void genericFunction(List<String> stringList){...}
void genericFunction(List<Integer> stringList){...}
Make some changes and try it again:
class GenericClass <E>{
void genericFunction(List<E> stringList) {
...
}
// some other code
}
...
GenericClass<String> genericClass=new GenericClass<String>(); // Genreric object
List<Integer> integerList= new ArrayList<Integer>();
integerList.add(100);
genericClass.genericFunction(integerList); // compile time error
Create methods in such a way
class GenericClass<E> {
private List<E> list = new ArrayList<E>();
public void addAll(List<E> newList) {
list.addAll(newList);
}
public void add(E e) {
list.add(e);
}
public E get(int index) {
return list.get(index);
}
// some other code
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…