实现retainAll()方法的最佳方法

时间:2015-09-25 08:06:08

标签: java

我有一个名为MyAbstractList的自定义类,它实现了MyList接口。这是代码:

public abstract class MyAbstractList<E> implements MyList<E> {
    protected int size = 0; // The size of the list

    protected MyAbstractList() {
    }

    protected MyAbstractList(E[] objects) {
        for (int i = 0; i < objects.length; i++)
            add(objects[i]);
    }

    public void add(E e) {
        add(size, e);
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public boolean addAll(MyList<E> otherList) {
        for (E e : otherList) {
            add(e);
        }
        if (otherList.size() > 0)
            return true;
        return false;
    }

    public boolean removeAll(MyList<E> otherList) {
        boolean removed = false;
        for (E e : otherList) {
            if (remove(e) && !removed)
                removed = true;
        }
        return removed;
    }

    public boolean remove(E e) {
        if (indexOf(e) >= 0) {
            remove(indexOf(e));
            return true;
        } else
            return false;
    }

    /** Retains the elements in this list that are also in otherList
     *  Returns true if this list changed as a result of the call      */
    public boolean retainAll(MyList<E> otherList) {

    }
}

如何实施retainAll()方法?

MyList界面:

public interface MyList<E> extends java.lang.Iterable<E> {
  /** Add a new element at the end of this list */
  public void add(E e);

  /** Add a new element at the specified index in this list */
  public void add(int index, E e);

  /** Clear the list */
  public void clear();

  /** Return true if this list contains the element */
  public boolean contains(E e);

  /** Return the element from this list at the specified index */
  public E get(int index);

  /** Return the index of the first matching element in this list.
   *  Return -1 if no match. */
  public int indexOf(E e);

  /** Return true if this list contains no elements */
  public boolean isEmpty();

  /** Return the index of the last matching element in this list
   *  Return -1 if no match. */
  public int lastIndexOf(E e);

  /** Remove the first occurrence of the element o from this list.
   *  Shift any subsequent elements to the left.
   *  Return true if the element is removed. */
  public boolean remove(E e);

  /** Remove the element at the specified position in this list
   *  Shift any subsequent elements to the left.
   *  Return the element that was removed from the list. */
  public E remove(int index);

  /** Replace the element at the specified position in this list
   *  with the specified element and returns the new set. */
  public Object set(int index, E e);

  /** Return the number of elements in this list */
  public int size();

  /** Adds the elements in otherList to this list.
  * Returns true if this list changed as a result of the call */
  public boolean addAll(MyList<E> otherList);

  /** Removes all the elements in otherList from this list
  * Returns true if this list changed as a result of the call */
  public boolean removeAll(MyList<E> otherList);

  /** Retains the elements in this list that are also in otherList
  * Returns true if this list changed as a result of the call */
  public boolean retainAll(MyList<E> otherList);

  /** Return an iterator for the list */
  public java.util.Iterator<E> iterator();
}

1 个答案:

答案 0 :(得分:1)

如果元素不是Comparable,则只能搜索参数中不存在的列表元素。

public boolean retainAll(MyList<E> otherList) {
    boolean changed = false;
    for (int i = size() - 1; i >= 0; i--) {
        Object obj = get(i);
        if (!otherList.contains(obj)) {
            remove(i);
            changed = true;
        }
    }
    return changed;
}

注意:此算法在O(n ^ 2)中完成,如果您有可比较列表,则可以转到O(n log(n))

第二个注意事项:不要使用迭代器来循环列表,因为对列表内容的更改可能会引发异常。

评论沙特的建议编辑: 没有必要更新size。这必须通过方法remove完成。