I am writing a custom iterator but I am seeing different warnings in my Java code.
Here is my code:
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class CustomIterator<E> implements Iterator<E> {
public static void main(String[] args) {
List<String> a = Arrays.asList("alpha", "beta", "gamma");
List<Integer> b = Arrays.asList(1, 2, 3);
// Type safety: The constructor CustomIterator(Iterator...) belongs to the raw type CustomIterator. References to generic type CustomIterator<E> should be parameterized
CustomIterator it = new CustomIterator(a.iterator(), b.iterator());
// some logic
}
// Type safety: Potential heap pollution via varargs parameter iterators
public CustomIterator(Iterator<E>... iterators) {
// some logic
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public E next() {
// TODO Auto-generated method stub
return null;
}
}
I just added the warnings as comments in above code.
It occurs at 2 places:
// Type safety: The constructor CustomIterator(Iterator...) belongs to the raw type CustomIterator. References to generic type CustomIterator<E> should be parameterized
CustomIterator it = new CustomIterator(a.iterator(), b.iterator());
and also here:
// Type safety: Potential heap pollution via varargs parameter iterators
public CustomIterator(Iterator<E>... iterators) {
// some logic
}
I can use @SuppressWarnings({ "unchecked", "rawtypes" })
to suppress them but I want to know why I am getting these and how to avoid them without suppressing.