I know if I had element and I want to get List/Set/Map with eith element only I should invoke :
Collections.singleton()
/ Collections.singletonList()
/ Collections.singletonMap()
I don't understand why all these methods contains singleton
in their names ?
I had assumption maybe it return same Collection for same object but this test shows that it is false
Object o = new Object();
System.out.println(Collections.singleton(o) == Collections.singleton(o));
output: 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
:
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.
See more on this question at Stackoverflow