简体   繁体   English

为什么在Set / List / Map中用于包装元素的方法的名称中包含'singleton'?(java.util.Collections)

[英]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.

相关问题 为什么菱形运算符不适用于Java 7中的java.util.Collections方法? - Why does the diamond operator not work for java.util.Collections methods in Java 7? java.util.Collections中同步方法的可用性 - usability of synchronized… methods in java.util.Collections 将java.util.Collections转换为Integer数组 - Convert java.util.Collections to Integer array java.util.Collections上的Java泛型警告 - Java generics warnings on java.util.Collections 导入java.util.Collections无法解析 - The import java.util.Collections cannot be resolved java.util.Collections $ UnmodifiableRandomAccessList到Collections.singletonList - java.util.Collections$UnmodifiableRandomAccessList to Collections.singletonList 为什么在java.util.Collections中声明了静态java.util.Collections.fill()方法而不是java.util.AbstractList中的实例方法? - Why is the static java.util.Collections.fill() method declared in java.util.Collections rather than an instance method in java.util.AbstractList? java.util.Collections $ UnmodifiableMap问题:包含代码 - java.util.Collections$UnmodifiableMap problem : code included 如何创建实现java.util.collections的类 - How to create a class that implements java.util.collections 如何索引java.util.Collections $ UnmodifiableMap的类类型? - How to index class type of a java.util.Collections$UnmodifiableMap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM