Java 中的最大大小列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6027995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum Size List in Java
提问by Chris Taylor
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.
Java 中的数据结构具有 List 的所有功能,但具有最大存储容量,并在添加新数据时删除旧数据,这对我很有用。可以想象,在某些时候我可能想要实现一个固定大小的队列,它保持数据的更一般的排序,并将旧数据丢弃在该排序中最低的位置,但这是未来的。
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...
}
Is there either (a) an existing data structure that serves my needs, or (b) a better way of implementing this data structure?
是否有(a)满足我需求的现有数据结构,或(b)实现此数据结构的更好方法?
采纳答案by Sean Patrick Floyd
Here's a List with a size limit, based on Guava's ForwardingList
:
这是一个具有大小限制的 List,基于Guava的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.
将其所有方法调用转发到另一个列表的列表。子类应该覆盖一个或多个方法来根据装饰器模式的需要修改后备列表的行为。
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.
Guava 为所有 JDK-5 集合类型提供了这样的基类。它们中的每一个都实现了相同的目的:使增加价值变得容易,同时将所有默认功能委托给底层集合。
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;
}
}
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)
它将所有实际功能委托给底层列表,默认情况下是一个 ArrayList(单参数构造函数),但您也可以提供(两个参数构造函数)
回答by jdevelop
I would use array and 2 indexes for head and tail of the list.Make sure that head is always < tail and you're safe.
我会使用数组和 2 个索引作为列表的头部和尾部。确保头部总是 < 尾部并且你是安全的。
回答by Tom Jefferys
Unless you want to use an actual array, I don't believe there is a list type data structure you can use.
除非您想使用实际数组,否则我不相信您可以使用列表类型的数据结构。
Personally I would extend one of the existing list classes to get the functionality, and override the add methods. This way you get all the other list operations for free. ie something like the following...
就我个人而言,我会扩展现有的列表类之一来获取功能,并覆盖添加方法。这样您就可以免费获得所有其他列表操作。即类似于以下内容...
public class FixedSizeArrayList<T> extends ArrayList<T> {
private final int maxSize;
public FixedSizeArrayList(int maxSize) {
super();
this.maxSize = maxSize
}
public boolean add(T t) {
if (size() >= maxSize) {
remove(0);
}
return super.add(t);
}
// implementation of remaining add methods....
}
回答by vichle
If you extend the LinkedList class you will have direct access to all it's methods. Instead of having to write stuff like
如果您扩展 LinkedList 类,您将可以直接访问它的所有方法。而不是必须写这样的东西
fixedList.getList().pop()
you could just write
你可以写
fixedList.pop()
You could then override the methods where you need to add the maxSize criteria.
然后,您可以覆盖需要添加 maxSize 条件的方法。