1

I have tested Collections.singleton() method, how to work, but i see that it is not work as what documentation say?

List arraylist= new ArrayList();
arraylist.add("Nguyen");
arraylist.add("Van");
arraylist.add("Jone");
List list = Collections.singletonList(arraylist);// contains three elements
System.out.println(list.size());// right

As what documentation say, The method call returns an immutable list containing only the specified object,A singleton list contains only one element and a singleton HashMap includes only one key. A singleton object is immutable (cannot be modified to add one more element),but when what thing i see in my code that list contains three elements("Nguyen","Van","Jone").

Anybody can explain for me why?? Thanks so much !!

M A
  • 71,713
  • 13
  • 134
  • 174
Quan Nguyen
  • 698
  • 1
  • 9
  • 19
  • Could you please provide a reference to your documentation? I can't find your source anywhere – Will Aug 16 '15 at 07:43
  • at here @ http://way2java.com/collections/collections-collections/singleton-list-with-singletonlist/. This is good website to learn Java, it is my think. – Quan Nguyen Aug 16 '15 at 08:07

1 Answers1

5

The returned List is a List of Lists. In this case, the returned list of lists itself is immutable, not the contained List. Also the returned list contains only one element, not three: the arraylist variable itself is considered an element and is the only element stored in the list returned by Collections.singletonList. In other words, the statement Collections.singletonList(arraylist) does not create a list that contains all elements of the provided list.

It would have been much more obvious if you use generics:

List<String> arraylist= new ArrayList<>();
arraylist.add("Nguyen");
arraylist.add("Van");
arraylist.add("Jone");
List<List<String>> list = Collections.singletonList(arraylist);

What the documentation says is that if you do the following:

List list = Collections.singletonList(arraylist);
list.add(new ArrayList());

then this would throw an exception at runtime.

M A
  • 71,713
  • 13
  • 134
  • 174
  • i confused that, A singleton list contains only one element. As what things you say then "A singleton list contains only one element" not correct, because one element is not List of List – Quan Nguyen Aug 16 '15 at 07:41
  • @QuanNguyen The returned list only contains _one_ element, which is the `arraylist` itself that is first created. It does not contain three elements. – M A Aug 16 '15 at 07:45
  • @QuanNguyen it's quite simple. Collections.singletonList(Foo foo) returns a List containing a single element: the foo passed as argument. You're passing an ArrayList as argument. So Collections.singletonList returns a List containing a single element: the arrayList you passed as argument. It thus returns a List. The single element is the ArrayList. – JB Nizet Aug 16 '15 at 07:46
  • @ Oke, i have understood, it's good explainations, thanks a lot. – Quan Nguyen Aug 16 '15 at 07:53
  • 2
    @QuanNguyen note that generics exist since 2004, and you're still not using them. It would help you if you used them: `List list = new ArrayList(); List> singleton = Collections.singletonList(list);` See how it's now simple to understand what each list contains. – JB Nizet Aug 16 '15 at 07:55
  • @JB Nizet, Thanks for offer. I know generic, i just write simple code to check singletonList, so i does not apply the generic to my code @@ – Quan Nguyen Aug 16 '15 at 07:59