Hello. I am writing a new concrete implementation of the Set interface, using the source code from java.util.TreeSet as a rough guide to the way I should lay my code out. Having completed the code, I compiled only to get the following error message from the compiler:
C:\ ... \ArraySet.java:7: name clash: add(E) in ArraySet<E> and add(E) in java.util.Set have the same erasure, yet neither overrides the other
public class ArraySet<E>
Here is some of my source code:
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ArraySet<E>
extends AbstractSet
implements Cloneable/*, java.io.Serializable*/ {
private transient ArrayList<E> list;
public ArraySet() {
list = new ArrayList<E>();
}
public ArraySet(Collection<? extends E> c) {
this();
addAll(c);
}
public Iterator<E> iterator() {
return list.iterator();
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public boolean add(E o) {
if (!list.contains(o))
return list.add( o );
return false;
}
public void add(int index, E o) {
if (!list.contains(o))
list.add( index, o );
}
// ...
and here is some of the code from TreeSet (minus the comments)
public class TreeSet<E>
extends AbstractSet<E>
implements SortedSet<E>, Cloneable, java.io.Serializable
{
private transient SortedMap<E,Object> m; // The backing Map
private transient Set<E> keySet; // The keySet view of the backing Map
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
private TreeSet(SortedMap<E,Object> m) {
this.m = m;
keySet = m.keySet();
}
public TreeSet() {
this(new TreeMap<E,Object>());
}
public TreeSet(Comparator<? super E> c) {
this(new TreeMap<E,Object>(c));
}
public TreeSet(Collection<? extends E> c) {
this();
addAll(c);
}
public TreeSet(SortedSet<E> s) {
this(s.comparator());
addAll(s);
}
public Iterator<E> iterator() {
return keySet.iterator();
}
public int size() {
return m.size();
}
public boolean isEmpty() {
return m.isEmpty();
}
public boolean contains(Object o) {
return m.containsKey(o);
}
public boolean add(E o) {
return m.put(o, PRESENT)==null;
}
// ...
As far as I can tell, my source code is doing the equivalent of everything that the TreeSet code is doing, yet I get this compiler error concerning the add() method. What am I doing wrong?
Thanks in advance
frag