Java列表只添加没有删除

时间:2017-02-24 15:55:31

标签: java collections

我想知道是否有可能创建Java java.util.List,它只允许添加元素但不允许删除元素?

我正在考虑重写删除方法的一种方法。请建议。

2 个答案:

答案 0 :(得分:4)

您可以扩展现有的List实现类并覆盖所有公共删除方法,但是有很多方法可以覆盖(可能更多,以下是我很快找到的)

public class UnDeletableList<E> extends ArrayList<E> {
    @Override
    public E remove(int index) {
        throw new UnsupportedOperationException("don't remove from this list");
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException("don't remove from this list");
    }

    @Override
    public boolean removeAll(Collection<?> o) {
        throw new UnsupportedOperationException("don't remove from this list");
    }

    @Override
    public boolean retainAll(Collection<?> o) {
        throw new UnsupportedOperationException("don't remove from this list");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("don't remove from this list");
    }

    // OPTIONAL IN CASE OF EXTRA STRICTNESS
    @Override
    public void replaceAll(UnaryOperator<E> u) {
        throw new UnsupportedOperationException("don't remove from this list");
   }

    @Override
    public E set(int i, E e) {
        throw new UnsupportedOperationException("don't remove from this list");
    }
}

答案 1 :(得分:3)

这可以使用Decorator模式实现。这样它就可以应用于实现List的所有容器:

private static class UnremovableList<E> implements  List<E> {
    private List<E> innerContainer;

    public UnremovableList(List<E> original) {
        innerContainer = original
    }

    @Override
    public void add(int location, E object) {
        innerContainer.add(location, object);
    }

    @Override
    public boolean add(E object) {
        return innerContainer.add(object);
    }

    @Override
    public boolean addAll(int location, Collection<? extends E> collection) {
        return innerContainer.addAll(location, collection);
    }

    @Override
    public boolean addAll(Collection<? extends E> collection) {
        return innerContainer.addAll(collection);
    } - 

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean contains(Object object) {
        return innerContainer.contains(object);
    }

    @Override
    public boolean containsAll(Collection<?> collection) {
        return innerContainer.containsAll(collection);
    }

    @Override
    public E get(int location) {
        return innerContainer.get(location);
    }

    @Override
    public int indexOf(Object object) {
        return innerContainer.indexOf(object);
    }

    @Override
    public boolean isEmpty() {
        return innerContainer.isEmpty();
    }

    @NonNull
    @Override
    public ListIterator<E> listIterator() {
        return listIterator(0);
    }

    @NonNull
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Iterator<E> iterator = innerContainer.iterator();

            @Override public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override public E next() {
                return iterator.next();
            }

            @Override public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    @Override
    public ListIterator<E> listIterator(final int location) {
        return new ListIterator<E>() {
            ListIterator<E> iterator = innerContainer.listIterator(location);

            @Override public void add(E object) {
                throw new UnsupportedOperationException();
            }

            @Override public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override public boolean hasPrevious() {
                return iterator.hasPrevious();
            }

            @Override public E next() {
                return iterator.next();
            }

            @Override public int nextIndex() {
                return iterator.nextIndex();
            }

            @Override public E previous() {
                return iterator.previous();
            }

            @Override public int previousIndex() {
                return iterator.previousIndex();
            }

            @Override public void remove() {
                throw new UnsupportedOperationException();
            }

            @Override public void set(E object) {
                throw new UnsupportedOperationException();
            }
        };
    }

    @Override
    public int lastIndexOf(Object object) {
        return innerContainer.lastIndexOf(object);
    }

    @Override
    public E remove(int location) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(Object object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        throw new UnsupportedOperationException();
    }

    @Override
    public E set(int location, E object) {
        return innerContainer.set(location, object);
    }

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

    @NonNull
    @Override
    public List<E> subList(int start, int end) {
        return new UnremovableList(innerContainer.subList(start, end));
    }

    @NonNull
    @Override
    public Object[] toArray() {
        return innerContainer.toArray();
    }

    @NonNull
    @Override
    public <T> T[] toArray(T[] array) {
        return innerContainer.toArray(array);
    }
}

用法:

List<String> stableList = new UnremovableList(Arrays.asList("A", "B", "C"));