java singletonList 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11205666/
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
What is the use of singletonList?
提问by mmoore
I was looking around for some elegant solution to removing null values from a List. I came across the following post, which says I can use list.removeAll(Collections.singletonList(null));
我一直在寻找一些优雅的解决方案来从列表中删除空值。我遇到了以下帖子,上面说我可以使用 list.removeAll(Collections.singletonList(null));
This, however, throws an UnsupportedOperationException
, which I'm assuming is because removeAll()
is attempting to do some mutative operation on the immutable singleton collection. Is this correct?
然而,这会抛出一个UnsupportedOperationException
,我假设这是因为removeAll()
它试图对不可变的单例集合进行一些可变操作。这个对吗?
If this is the case, what would be a typical use of this singletonList
? To represent a collection of size 1 when you're sure you don't want to actually do anything with the collection?
如果是这种情况,它的典型用途是singletonList
什么?当您确定不想实际对集合执行任何操作时,要表示大小为 1 的集合?
Thanks in advance.
提前致谢。
回答by Tomasz Nurkiewicz
It works like a charm:
它的作用就像一个魅力:
List<String> list = new ArrayList<String>();
list.add("abc");
list.add(null);
list.add("def");
list.removeAll(Collections.singletonList(null));
System.out.println(list); //[abc, def]
Indeed Collections.singletonList(null)
is immutable (which is unfortunately hidden in Java[1]), but the exception is thrown from your list
variable. Apparently it is immutable as well, like in example below:
IndeedCollections.singletonList(null)
是不可变的(不幸的是隐藏在 Java [1] 中),但异常是从您的list
变量中抛出的。显然它也是不可变的,如下例所示:
List<String> list = Arrays.asList("abc", null, "def");
list.removeAll(Collections.singletonList(null));
This code willthrow an UnsupportedOperationException
. So as you can see singletonList()
is useful in this case. Use it when client code expects a read-only list (it won't modify it) but you only want to pass one element in it. singletonList()
is (thread-)safe (due to immutability), fast and compact.
此代码将抛出一个UnsupportedOperationException
. 正如你所看到的singletonList()
,在这种情况下很有用。当客户端代码需要一个只读列表(它不会修改它)但您只想在其中传递一个元素时使用它。singletonList()
是(线程)安全的(由于不变性),快速且紧凑。
[1]E.g. in scalathere is a separete hierarchy for mutable and immutable collections and API can choose whether it accept this or the other (or both, as they have common base interfaces)
[1]例如,在Scala 中,可变和不可变集合有一个单独的层次结构,API 可以选择接受这个或另一个(或两者,因为它们有共同的基本接口)
回答by mihaisimi
Has your list been protected with
您的名单是否受到保护
Collections.unmodifiableList(list)
Because if you have protected it and try to modify it later you get that error.
因为如果您保护了它并稍后尝试对其进行修改,则会出现该错误。
回答by daniu
To answer your actual question :
要回答您的实际问题:
what would be a typical use of this singletonList? To represent a collection of size 1 when you're sure you don't want to actually do anything with the collection?
这个 singletonList 的典型用途是什么?当您确定不想实际对集合执行任何操作时,要表示大小为 1 的集合?
The typical use is if you have one element and want to pass it to a method that accepts a List
, ie
典型的用法是,如果您有一个元素并希望将其传递给接受 a 的方法List
,即
public void registerUsers(List<User> users) {...}
User currentUser = Login Manager.getCurrentUser();
registerUsers(Collections.singletonList(currentUser));
The removeAll()
is a special case for this.
这removeAll()
是一个特例。