自定义迭代器removeIf不会删除元素

时间:2017-12-01 18:34:01

标签: java iterator

我在项目中使用自定义os.path.expanduser()到自定义Iterator结构。但是,当迭代LinkedList时,迭代器不会从给定列表中删除该条目。

我的自定义迭代器:

LinkedList

我的package plotter.structures; import java.util.Iterator; import java.util.NoSuchElementException; /** * * @author croemheld * * @param <T> The type of the elements of the {@link LinkedList}. */ public class LinkedListIterator<T> implements Iterator<T> { /** * Previous element in the {@link LinkedListIterator}. */ private ListNode<T> previous = null; /** * Current element in the {@link LinkedListIterator}. */ private ListNode<T> current; private LinkedList<T> collection; /** * Returns an instance of {@link LinkedListIterator} which iterates from the head * to the tail of a {@link LinkedList}. * * @param collection The collection to iterate through. */ public LinkedListIterator(LinkedList<T> collection) { this.collection = collection; this.current = collection.getHead(); } /** * Returns a {@link LinkedListIterator} which starts at a specific index. * * @param collection The {@link LinkedList} for this {@link LinkedListIterator}. * @param index The index to start at. */ public LinkedListIterator(LinkedList<T> collection, int index) { this.current = collection.get(index); } @Override public boolean hasNext() { return this.current != null; } @Override public T next() { if(!hasNext()) { throw new NoSuchElementException(); } T element = this.current.getElement(); this.previous = this.current; this.current = this.current.getNext(); return element; } @Override public void remove() { if(this.previous.getPrev() != null) { this.previous.getPrev().setNext(this.previous.getNext()); } this.current.setPrev(this.previous.getPrev()); } /** * * @return The next element of this {@link LinkedListIterator}. */ public T peek() { if(!hasNext()) { return null; } return this.current.getElement(); } /** * Determines if the next element is null or equal to the current element. * * @param element The element of the current iteration. * * @return True, if the next element is equal to the current one or if the next element is null. */ public boolean hasEqualFollower(T element) { return (peek() != null && !peek().equals(element) || peek() == null); } } 缩减为无法正常工作的方法:

LinkedList

public class LinkedList<T> implements Collection<T> { /** * The head {@link ListNode} of this {@link LinkedList}. */ private ListNode<T> head; /** * The tail {@link ListNode} of this {@link LinkedList}. */ private ListNode<T> tail; /** * The length of this {@link LinkedList}. */ private int size; // [...] @Override public boolean removeIf(Predicate<? super T> predicate) { int count = size(); LinkedListIterator<T> iterator = new LinkedListIterator<T>(this); while(iterator.hasNext()) { T element = iterator.next(); if(predicate.test(element)) { iterator.remove(); } } return size() < count; } // [..] } 正确地为所述元素返回true,但它不会从我的predicate.test(element)中删除该元素。

调用如下所示:

LinkedList

此时result.removeIf(node -> node.getValue().equals(ComplexNumber.ZERO.toString())); 方法比较两个equals个对象,两个对象的值均为String(复数表示)并正确返回。

为什么在(0.0 + 0.0i)调用之后元素仍然在列表中?

0 个答案:

没有答案