*
The moose likes Beginning Java and the fly likes what does collections.singletonlist do Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what does collections.singletonlist do" Watch "what does collections.singletonlist do" New topic
Author

what does collections.singletonlist do

Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
HI all

what does the collections.singletonlist(Object o) method do?

What do we mean by the term immutable list and singleton list?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

An immutable List (also referred to as an unmodifiable List) cannot have it's contents changed. The methods to add or remove items will throw exceptions if you tr to alter the contents.

A singleton List contains only that item and cannot be altered.
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
Ya I understood the terms.thanks
I came across this code.But why do we create a list with a single object.
Can you say what is the advantage in using collections.singletonlist() ?

Thanks
Praveen SP
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you want an empty list, you can use Collections.EMPTY_LIST or emptyList().
If you want an unmodifiable List containing a single item, compare Collections.singletonlist("My Object") to the code doing the same.
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
thanks for your answer david
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does collections.singletonlist do
 
Similar Threads
ThreadSafe Collection
web service security issue
Purpose of Wild Card in Reference
File list() instance method
Finding a Tree Node