I'm writing a skiplist class in java as an excercise. I've written a class called SkipListInternal<E>
which contains the actual skiplist. I've also made a wrapper class called SkipListSet<E>
which implement the SortedSet<E>
interface and contains an instance of SkipListInternal<E>
.
Among other things, SkipListInternal<E>
contains a method E find(E e)
which returns the element equal to e
if it is present, and otherwise returns null.
When writing the boolean contains(Object o)
(inherited from Collection<E>
via SortedSet<E>
) method I noticed that its argument is an Object and not an E. I intended to do something like this, but is not possible due to type erasure:
public class SkipListSet<E> implements SortedSet<E>{
private SkipListInternal<E> skiplist;
...
public boolean contains(Object o){
if (!(o instanceof E)) return false;
return skiplist.find((E) o) != null;
}
...
}
Since it can't be done this way, how should I do it?
See Question&Answers more detail:os