[英]Why does methods for wrapping element in Set/List/Map contains 'singleton' in name ?(java.util.Collections)
I know if I had element and I want to get List/Set/Map with eith element only I should invoke : 我知道我是否有元素,并且只想使用eith元素获取List / Set / Map,我应该调用:
Collections.singleton()
/ Collections.singletonList()
/ Collections.singletonMap()
Collections.singleton()
/ Collections.singletonList()
/ Collections.singletonMap()
I don't understand why all these methods contains singleton
in their names ? 我不明白为什么所有这些方法在名称中都包含
singleton
?
I had assumption maybe it return same Collection for same object but this test shows that it is false 我假设也许它为相同的对象返回相同的Collection,但是此测试表明它是错误的
Object o = new Object();
System.out.println(Collections.singleton(o) == Collections.singleton(o));
output: false 输出:false
For me singleton pattern forbid to create more than one element of concrete type and provided unify gateway for acces to this object. 对我而言,单例模式禁止创建多个具体类型的元素,并为该对象提供了访问权限的统一网关。
please clarify. 请澄清。
It's a different meaning of the word "singleton" - it's not "the singleton pattern", it's "create a collection from a single item", that's all. 这是“单身”一词的不同含义-不是“单身模式”,而是“从单个项目创建收藏集”,仅此而已。
For example, from define:singleton
: 例如,来自
define:singleton
:
a single person or thing of the kind under consideration.
正在考虑的单个人或事物。
Think of it this way: "singleton" is to "one thing" as "pair" is to "two things". 这样想:“单身”是“一件事”,“一对”是“两件事”。 So it wouldn't be entirely unnatural to have
Collections.pair(T t1, T t2)
in the same way. 因此,以相同的方式拥有
Collections.pair(T t1, T t2)
并非完全不自然。
这些方法背后的想法是,集合中的元素是一个单一的元素,而且将永远是-你不能从集合中删除它,也不能添加其他项目到这个集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.