Q:

Implementing a Set collection using ArrayList

Im having a hard time implementing a set collection using arrayList.
I don't want to use a HashSet. And i would like an someone to explain me please why it is not working
set.java:
public class set<E> {
    Set <E> setList;
    ArrayList<E> elementList;

    public set(){

        this.setList = (Set<E>) new ArrayList();

    }

    public set(E[] inputArray){
        super();
        for(E element: inputArray)
            setList.add( element);
    }

my main.java:
Integer a[]= {4,2,9,2,2,2,52};
set aSet = new set(a);
System.out.println(aSet.toString());

i get an error:
java.util.ArrayList cannot be cast to java.util.Set

A:

You cannot cast  ArrayList to Set, they are at the same level in inheritance tree (like brothers), Not sure what are you trying to achieve by this

If you want to create HashSet out of ArrayList use the constructor
new HashSet<SomeType>(arrayListInstance);

as @nacho mentioned, You will need to implement Set interface and name your implementation some meaning full name (for example ListBackedSet or ArraySet)
If you want to create new implementation of Set backed by ArrayList, You need to preserve uniqueness property and for that you will have to customize add(), addAll() and constructors to avoid duplication

