是否存在不允许空值的基本Java集实现?
Java接口的API说明: 例如,有些实现禁止是否存在不允许空值的基本Java集实现?,java,api,null,set,Java,Api,Null,Set,Java接口的API说明: 例如,有些实现禁止null元素,有些实现对其元素的类型有限制 我正在寻找一个基本的集合实现,它不需要排序(如接口所提供的),并且不允许null,并且都允许空元素。此外,TreeSet还要求元素实现 目前似乎不存在这样的基本集合。有人知道为什么吗?或者如果真的存在,我能在哪里找到它 [编辑]:我不希望允许nulls,因为稍后在代码中,我的类将迭代集合中的所有元素并调用特定方法。(我实际上使用的是HashSet)。我宁愿快速失败,也不愿稍后失败,或者由于null出现在集合
null
元素,有些实现对其元素的类型有限制
我正在寻找一个基本的集合实现,它不需要排序(如接口所提供的),并且不允许null
,并且都允许空元素。此外,TreeSet还要求元素实现
目前似乎不存在这样的基本集合
。有人知道为什么吗?或者如果真的存在,我能在哪里找到它
[编辑]:我不希望允许
null
s,因为稍后在代码中,我的类将迭代集合中的所有元素并调用特定方法。(我实际上使用的是HashSet)。我宁愿快速失败,也不愿稍后失败,或者由于null
出现在集合中而意外引发一些奇怪的行为。通过对适当的现有类进行子类化,您可以轻松编写自己的类,覆盖所有相关的方法,这样就不能添加null
元素。我不确定哪种类型是正确的。但是您不能从您选择的集合或哈希表继承并重写Add方法,如果元素为null则引发异常吗?为什么不允许null
如果将null
添加到集合中,是否要引发异常?如果是这样,就这样做:
private Set<Object> mySet = new HashSet<Object>() {
@Override
public boolean add(Object e) {
if (e == null)
throw new IllegalArgumentException("null"); // or NPE
// or, of course, you could just return false
return super.add(e);
}
};
private Set mySet=new HashSet(){
@凌驾
公共布尔加法(对象e){
如果(e==null)
抛出新的IllegalArgumentException(“null”);//或NPE
//当然,也可以返回false
返回super.add(e);
}
};
HashSet
的addAll()
反复调用add()
,因此这是您必须重写的唯一方法。比扩展特定实现更好的是,您可以轻松编写Set
的代理实现来检查null
s。这类似于集合.checkedSet
。除了适用于任何实现之外,您还可以确保您已经覆盖了所有适用的方法。通过扩展具体的集合发现了许多缺陷,这些集合在以后的版本中添加了额外的方法。我想说的是使用组合而不是继承。。。这可能需要更多的工作,但在Sun可能对集合框架进行的任何更改面前,它将更加稳定
public class NoNullSet<E> implements Set<E>
{
/** The set that is wrapped. */
final private Set<E> wrappedSet = new HashSet<E>();
public boolean add(E e)
{
if (e == null)
throw new IllegalArgumentException("You cannot add null to a NoNullSet");
return wrappedSet.add(e);
}
public boolean addAll(Collection<? extends E> c)
{
for (E e : c) add(e);
}
public void clear()
{ wrappedSet.clear(); }
public boolean contains(Object o)
{ return wrappedSet.contains(o); }
... wrap the rest of them ...
}
public类NoNullSet实现Set
{
/**包装好的那套*/
最终私有集wrappedSet=newhashset();
公共布尔加法(E)
{
如果(e==null)
抛出新的IllegalArgumentException(“不能将null添加到非空集”);
返回wrappedSet.add(e);
}
公共布尔加法(Collection您可能也希望签出。我相信它们更害怕空值。您可以使用apache collections和its,并将谓词设置为不允许空值。如果有人发送空值,您将获得异常。这是一种失败的通用方法-您提供了一个过滤器实现,可以限制获得空值的内容以您想要的方式进行DDE。查看java.util.Collections的源代码,了解包装的想法(我认为我对FilteredCollection类的实现是正确的……但它没有经过扩展测试)。最后有一个示例程序显示了其用法
public interface Filter<T>
{
boolean accept(T item);
}
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
public class FilteredCollections
{
private FilteredCollections()
{
}
public static <T> Collection<T> filteredCollection(final Collection<T> c,
final Filter<T> filter)
{
return (new FilteredCollection<T>(c, filter));
}
private static class FilteredCollection<E>
implements Collection<E>,
Serializable
{
private final Collection<E> wrapped;
private final Filter<E> filter;
FilteredCollection(final Collection<E> collection, final Filter<E> f)
{
if(collection == null)
{
throw new IllegalArgumentException("collection cannot be null");
}
if(f == null)
{
throw new IllegalArgumentException("f cannot be null");
}
wrapped = collection;
filter = f;
}
public int size()
{
return (wrapped.size());
}
public boolean isEmpty()
{
return (wrapped.isEmpty());
}
public boolean contains(final Object o)
{
return (wrapped.contains(o));
}
public Iterator<E> iterator()
{
return new Iterator<E>()
{
final Iterator<? extends E> i = wrapped.iterator();
public boolean hasNext()
{
return (i.hasNext());
}
public E next()
{
return (i.next());
}
public void remove()
{
i.remove();
}
};
}
public Object[] toArray()
{
return (wrapped.toArray());
}
public <T> T[] toArray(final T[] a)
{
return (wrapped.toArray(a));
}
public boolean add(final E e)
{
final boolean ret;
if(filter.accept(e))
{
ret = wrapped.add(e);
}
else
{
// you could throw an exception instead if you want -
// IllegalArgumentException is what I would suggest
ret = false;
}
return (ret);
}
public boolean remove(final Object o)
{
return (wrapped.remove(o));
}
public boolean containsAll(final Collection<?> c)
{
return (wrapped.containsAll(c));
}
public boolean addAll(final Collection<? extends E> c)
{
final E[] a;
boolean result;
a = (E[])wrapped.toArray();
result = false;
for(final E e : a)
{
result |= wrapped.add(e);
}
return result;
}
public boolean removeAll(final Collection<?> c)
{
return (wrapped.removeAll(c));
}
public boolean retainAll(final Collection<?> c)
{
return (wrapped.retainAll(c));
}
public void clear()
{
wrapped.clear();
}
public String toString()
{
return (wrapped.toString());
}
}
}
import java.util.ArrayList;
import java.util.Collection;
public class Main
{
private static class NullFilter<T>
implements Filter<T>
{
public boolean accept(final T item)
{
return (item != null);
}
}
public static void main(final String[] argv)
{
final Collection<String> strings;
strings = FilteredCollections.filteredCollection(new ArrayList<String>(),
new NullFilter<String>());
strings.add("hello");
strings.add(null);
strings.add("world");
if(strings.size() != 2)
{
System.err.println("ERROR: strings.size() == " + strings.size());
}
System.out.println(strings);
}
}
公共接口过滤器
{
布尔接受(T项);
}
导入java.io.Serializable;
导入java.util.Collection;
导入java.util.Iterator;
公共类筛选器集合
{
私有筛选集合()
{
}
公共静态集合filteredCollection(最终集合c,
最终过滤器(过滤器)
{
返回(新筛选集合(c,筛选));
}
私有静态类FilteredCollection
执行集合,
可序列化
{
私人最终收藏包装;
专用最终过滤器;
FilteredCollection(最终集合集合,最终筛选器f)
{
if(集合==null)
{
抛出新的IllegalArgumentException(“集合不能为null”);
}
如果(f==null)
{
抛出新的IllegalArgumentException(“f不能为null”);
}
包装=收集;
滤波器=f;
}
公共整数大小()
{
返回(wrapped.size());
}
公共布尔值为空()
{
return(wrapped.isEmpty());
}
公共布尔包含(最终对象o)
{
退货(包装,包含(o));
}
公共迭代器迭代器()
{
返回新的迭代器()
{
最后一个迭代器顺便说一句,如果您要求一个不允许空值的Map
实现,旧的java.util.Hashtable
不允许空值。Hashtable不允许空值……-com.google.common.collect.ImmutableSet
的文档中:
具有可靠的、用户指定的迭代顺序的高性能、不可变的集合。不允许空元素。
在这个特定的问题/示例中,如果您有一个HashSet mySet
callmySet.remove(空)
在开始对您提到的所有元素进行迭代之前?没有忽略或约束null的基本专有集合实现!有,但这是为包含枚举类型而定制的
但是,如果使用或,可以避免创建自己的实现:
1.番石榴溶液:
Set noNulls = Constraints.constrainedSet(new HashSet(), Constraints.notNull());
Set noNulls = new HashSet();
CollectionUtils.addIgnoreNull(noNulls, object);
2.公共收藏:
Set noNulls = Constraints.constrainedSet(new HashSet(), Constraints.notNull());
Set noNulls = new HashSet();
CollectionUtils.addIgnoreNull(noNulls, object);
对我来说,我没有找到,
所以我超越了
[null, test]
[test]
test