在Java中的最大尺寸列表 [英] Maximum Size List in Java

查看:142
本文介绍了在Java中的最大尺寸列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我有用的Java中的数据结构,它具有一个列表的所有功能,但具有的最大存储容量,并丢弃当加入较新的数据较旧的数据。可以想象在某些时候我可能要实现一个固定大小的队列这使数据更一般的顺序,并丢弃最低在订货旧的数据,但是这是以后的事。

It's useful to me to have a data structure in Java that has all the functionality of a List, but has a maximum storage capacity, and drops older data when newer data is added. Conceivably at some point I might want to implement a fixed size Queue which keeps a more general ordering of the data, and drops the old data lowest in that ordering, but that's the for the future.

目前,我正在实现这样的:

At the moment I'm implementing it like this:

public class FixedSizeList<T> {

  private final int maxSize;
  private final LinkedList<T> list = new LinkedList<T>();

  public FixedSizeQueue(int maxSize) {
    this.maxSize = maxSize < 0 ? 0 : maxSize;
  }

  public T add(T t) {
    list.add(t);
    return list.size() > maxSize ? list.remove() : null;
  }

  // add remaining methods...

}

有没有供应我的需要,或(b)实现这种数据结构的一种更好的方法是(a)现有的数据结构?

Is there either (a) an existing data structure that serves my needs, or (b) a better way of implementing this data structure?

推荐答案

下面是一个大小限制名单的基础上,番石榴的<一个href=\"http://guava-libraries.google$c$c.com/svn/trunk/javadoc/index.html?com/google/common/collect/ForwardingList.html\"><$c$c>ForwardingList:

Here's a List with a size limit, based on Guava's ForwardingList:

该转发其所有方法的列表
  调用另一个列表。子类
  应覆盖一个或多个方法,以
  修改为后盾的行为
  每装饰为所需的列表
  格局。

A list which forwards all its method calls to another list. Subclasses should override one or more methods to modify the behavior of the backing list as desired per the decorator pattern.

番石榴具有基础类这对所有JDK-5系列的类型。他们每个人都履行同样的目的:使它容易增加价值,而所有委托默认功能为基础集合

Guava has base classes like this for all JDK-5 Collection types. Each of them fulfills the same purpose: making it easy to add value, while delegating all default functionality to the underlying collection.

public class LimitingList<E> extends ForwardingList<E> {

    private final class LimitingListIterator extends ForwardingListIterator<E> {

        private final ListIterator<E> innerListIterator;

        private LimitingListIterator(final ListIterator<E> innerListIterator) {
            this.innerListIterator = innerListIterator;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void add(final E element) {
            if (inner.size() < maxSize)
                innerListIterator.add(element);
            else
                throw new IndexOutOfBoundsException();
        }

        @Override
        protected ListIterator<E> delegate() {
            return innerListIterator;
        }
    }

    public LimitingList(final int maxSize) {
        this(new ArrayList<E>(), maxSize);
    }

    public LimitingList(final List<E> inner, final int maxSize) {
        super();
        this.inner = inner;
        this.maxSize = maxSize;
    }

    @Override
    public boolean addAll(final Collection<? extends E> collection) {
        boolean changed = false;
        for (final E item : collection) {
            final boolean tmpChanged = add(item);
            changed = changed || tmpChanged;
            if (!tmpChanged)
                break;
        }
        return changed;
    }

    @Override
    public boolean add(final E e) {
        if (inner.size() < maxSize)
            return super.add(e);
        else
            return false;
    }

    @Override
    public ListIterator<E> listIterator() {
        return new LimitingListIterator(inner.listIterator());
    }

    @Override
    public void add(final int index, final E element) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addAll(final int index, final Collection<? extends E> elements) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ListIterator<E> listIterator(final int index) {
        return new LimitingListIterator(inner.listIterator(index));
    }

    private final int maxSize;
    private final List<E> inner;

    @Override
    protected List<E> delegate() {
        return inner;
    }

}

这代表所有实功能的基础列表,这是每默认(单参数的构造函数)一个ArrayList,但你也可以提供(二参数的构造函数)

It delegates all real functionality to an underlying list, which is an ArrayList per default (single argument constructor), but you can also supply (two argument constructor)

这篇关于在Java中的最大尺寸列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆