以任何方式设置集合的最大大小? [英] Any way to set max size of a collection?

查看:186
本文介绍了以任何方式设置集合的最大大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在Java中设置最大集合大小?

解决方案



 列表< X> list = Arrays.asList(new X [desiredSize]); 
//其中X是任何对象类型(包括数组和枚举,
//但不包括原语)

生成的列表是可修改的,但不可调整大小(即 add(e) remove(e) 无效,但set(index,e)








或者:使用 Guava ,这里是一个静态方法,装饰现有的列表,最大大小

  public static< T>列表< T> setMaxSize(
final List< T> input,final int maxSize){

return new ForwardingList< T>(){

@Override
public boolean addAll(Collection< ;? extends T> collection){
return standardAddAll(collection);
}

@Override
public boolean addAll(int index,Collection< ;? extends T> elements){
return standardAddAll(index,elements);
}

public boolean add(T e){
checkMaxSize();
return delegate()。add(e);
}

@Override
public void add(final int index,final T e){
checkMaxSize();
delegate()。add(index,e);
}

private void checkMaxSize(){
if(size()> = maxSize){
throw new UnsupportedOperationException(Maximum Size
+ maxSize +reached);
}
}

@Override
protected List< T> delegate(){
return input;
}
};
}

由于所有标准集合类型都存在ForwardingXxx类,所以您可以自己写类似的装饰器



显然,只有在您的客户端代码使用修饰集合时,这才会有效。如果您更改了底层集合(与Collections.unmodifiableXXX方法一样)。



参考:




is there any way to set maximum size of collection in Java?

解决方案

You can do this:

List<X> list = Arrays.asList(new X[desiredSize]);
// where X is any Object type (including arrays and enums,
// but excluding primitives)

The resulting list is modifiable, but not resizable (i.e. add(e) and remove(e) don't work, but set(index, e) does).

Reference:


Or: using Guava, here's a static method that decorates an existing List with a maximum size

public static <T> List<T> setMaxSize(
    final List<T> input, final int maxSize){

    return new ForwardingList<T>(){

        @Override
        public boolean addAll(Collection<? extends T> collection){
            return standardAddAll(collection);
        }

        @Override
        public boolean addAll(int index, Collection<? extends T> elements){
            return standardAddAll(index, elements);
        }

        public boolean add(T e) {
            checkMaxSize();
            return delegate().add(e);
        }

        @Override
        public void add(final int index, final T e){
            checkMaxSize();
            delegate().add(index, e);
        }

        private void checkMaxSize(){
            if(size() >= maxSize){
                throw new UnsupportedOperationException("Maximum Size "
                    + maxSize + " reached");
            }
        }

        @Override
        protected List<T> delegate(){
            return input;
        }
    };
}

Since ForwardingXxx classes exist for all standard collection types, you can write yourself similar decorators for other collections as well.

Obviously this will only work if your client code uses the decorated collection. If you change the underlying collection you are screwed (just like the Collections.unmodifiableXXX methods)

Reference:

这篇关于以任何方式设置集合的最大大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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