set.iterator().......where am i wrong?
<p>Trying to implement a SET interface and to use an Iterator for the union method. Inside the union method it never enters the while loop. It does not add the elements from the parameter "set" that the union method has.
Any ideas? :)</p>

<p>package linkedSet;</p>

<p>import java.util.Iterator;
import java.util.NoSuchElementException;</p>

<p>import linkedList.LinearNode;</p>

<p>public class LinkedSet implements SetADT {</p>

<pre><code>private int size;
private LinearNode&lt;T&gt; front;
private boolean allowNullElement = false;

// /////////////////////////////////////////////////////
private class LinkedSetIterator implements Iterator&lt;T&gt; {

    private LinearNode&lt;T&gt; currentNode;

    public LinkedSetIterator() {
        currentNode = front;
    }

    @Override
    public boolean hasNext() {
        return currentNode == null;
    }

    @Override
    public T next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        T temp = currentNode.getElement();
        currentNode = currentNode.getNext();
        return temp;
    }
}
///////////////////////////////////////////////////
@Override
public Iterator&lt;T&gt; iterator() {
    return new LinkedSetIterator();

}
public LinkedSet(boolean allowNullElement) {
    size = 0;
    front = new LinearNode&lt;T&gt;();
    this.allowNullElement = allowNullElement;
}

public String toString() {

    String str = "{";
    for (int i = 0; i &lt; size; i++) {
        str += getNode(i).getElement();
        if (i &lt; size - 1) {
            str += (", ");
        }
    }
    return str + "}";

}

private LinearNode&lt;T&gt; getNode(int index) {
    LinearNode&lt;T&gt; current = front;
    for (int i = 0; i &lt; index; i++) {
        current = current.getNext();
    }
    return current;
}

@Override
public void add(T element) {
    if (element == null &amp;&amp; !allowNullElement) {
        throw new IllegalArgumentException(
                "Null element is not allowed to add");
    }
    front = new LinearNode&lt;T&gt;(element, front);
    size++;

}

@Override
public T remove(T element) {

    if (!(contains(element))) {
        throw new IllegalStateException();
    }
    T temp = null;
    ;
    for (int i = 0; i &lt; size; i++) {

        temp = getNode(i).getElement();
        if (temp.equals(element) || temp == element) {
            if (i == 0) {
                front = front.getNext();
                break;

            } else {
                getNode(i).setElement(front.getElement());
                front = front.getNext();
                break;
            }
        }
    }
    size--;
    return temp;
}

@Override
public boolean contains(T element) {
    for (int i = 0; i &lt; size; i++) {
        if (getNode(i).getElement() != null
                &amp;&amp; getNode(i).getElement().equals(element)) {
            return true;
        }
        if (getNode(i).getElement() == null
                &amp;&amp; getNode(i).getElement() == element) {
            return true;
        }
    }
    return false;
}

@Override
public boolean isEmpty() {

    return size == 0;
}

@Override
public int size() {
    return size;
}

@Override
public boolean isSubset(SetADT&lt;T&gt; set) {
    if (set == null) {
        throw new IllegalArgumentException();
    }

    for (int i = 0; i &lt; size; i++) {
        T temp = getNode(i).getElement();
        if (!(set.contains(temp))) {
            return false;
        }
    }
    return true;
}

@Override
public SetADT&lt;T&gt; intersection(SetADT&lt;T&gt; set) {
    if (set == null) {
        throw new IllegalArgumentException();
    }
    LinkedSet&lt;T&gt; temp = new LinkedSet&lt;T&gt;(allowNullElement);

    for (int i = 0; i &lt; size; i++) {

        if (set.contains(getNode(i).getElement())) {
            temp.add(getNode(i).getElement());
        }
    }
    return temp;
}

public SetADT&lt;T&gt; union(SetADT&lt;T&gt; set) {

    if (set == null) {
        throw new IllegalArgumentException();
    }

    LinkedSet&lt;T&gt; temp = new LinkedSet&lt;T&gt;(allowNullElement);
    LinearNode&lt;T&gt; node = front;

    for (int i = 0; i &lt; size; i++) {
        T el = node.getElement();
        temp.add(el);
        node = node.getNext();
    }

    Iterator&lt;T&gt; iterator = set.iterator();
    while (iterator.hasNext()) {
        temp.add(iterator.next());

    }
    return temp;
}
</code></pre>

<p>}</p>