What is the benefit for Collections.singleton() to return a Set instead of a Collection?

Related searches

The Collections.singleton() method returns a Set with that single argument instead of a Collection.

Why is that so? From what I can see, apart from Set being a subtype of Collection, I can see no advantage... Is this only because Set extends Collection anyway so there is no reason not to?

And yes, there is also Collections.singletonList() but this is another matter since you can access random elements from a List with .get()...

I'm not sure there's a "benefit" or "advantage" per se? It's just the method that returns a singleton Set, and happens to be the default implementation when you want a singleton Collection as well, since a singleton Collection happens to be a mathematical set as well.

Convenience Implementations (The Java™ Tutorials > Collections , Collections.singleton() method is a java.util. public static Set singleton(T obj) and public static List singletonList(T obj) Parameters: obj : the sole object to Return: an immutable list/set containing only the specified object. The only benefit I can think of would be that methods that accept a Set would not accept a Collections.singleton () had it returned a simple Collection. – Petr Janeček Jul 23 '15 at 23:27. @Slanec indeed Whereas methods accepting a Collection will happily accept a Set as well – fge Jul 23 '15 at 23:43.


The benefit is found in the first adjective read in that JavaDoc documentation: immutable.

There are times when you are working with code that demands a Set (or List, etc.). In your own context you may have a strict need for only a single item. To accomplish your own goal of enforcing the rule of single-item-only while needing to present that item in a set, use a Set implementation that forbids you from adding more than one item.

"Immutable" on Collections::singleton means that, once created, the resulting Set object is guaranteed to have one, and only one item. Not zero, and not more than one. No more can be added. The one item cannot be removed.

For example, imagine your code is working with an Employee object representing the CEO (Chief Executive Officer) of your company. Your code is explicitly dealing with the CEO only, so you know there can be only one such Employee object at a time, always one CEO exactly. Yet you want to leverage some existing code that creates a report for a specified collection of Employee objects. By using Collection.singleton you are guaranteed that your own code does not mistakenly have other than one single employee, while still being able to pass a Set.

Set< Employee > ceo = Collections.singleton( new Employee( "Tim Cook" ) ) ;  // Always exactly one item in this context, only one CEO is possible.

ceo.add( … ) ;     // Fails, as the collection is immutable.
ceo.clear() ;      // Fails, as the collection is immutable.
ceo.remove( … ) ;  // Fails, as the collection is immutable.

someReport.processEmployees( ceo ) ;
Java 9: Set.of & List.of

Java 9 and later offers new interface methods Set.of and List.of to th same effect, an immutable collection of a single element.

Set< Pet > pet = Set.of( someDog ) ;

But sibling of methods are overloaded to accept any number of elements to be in the immutable collection, not just one element.

Set< Pet > pets = Set.of( someDog , someOtherDog , someCat ) ;

Collections.singleton() method in Java with example, Ultimately, for Java 8 and earlier, Collections::singletonList is the best keeps us in suspense about what collection type it will return until the final four letters. toUnmodifiableList() is the same type that is returned by List::of Collections$ SingletonList , which does not support the set method on its� 5 What is the benefit for Collections.singleton() to return a Set instead of a Collection? Jun 9 '17. 5 Gradle task for Java playground Jul 29 '16.

Singleton List Showdown: Collections::singletonList Vs. List::of , o − This is the sole object to be stored in the returned set. Return Value. The method call returns an immutable set containing only the specified object. Exception. 6 What is the benefit for Collections.singleton() to return a Set instead of a Collection? View more network posts → Top tags (5) kotlin. Score 0. Posts 1

Not all the lists are random access, just take a LinkedList (random access in the API, not in the implementation)as a counter-example. By the way I agree with Louis Wasserman, a Set simply makes sense because it is closer to the mathematical definition, it just feels natural.

java.util.Collections.singleton() Method, singletonList() Method The singletonList(T) method is used to return an immutable list containing o − This is the sole object to be stored in the returned list. Thus, the return value of toArray(T[]) needs to return an array: the argument array, if it was large enough, or the newly allocated array. But now we have another problem. We no longer have a way to tell the caller the number of elements that were copied from the collection into the array.

The reason singleton Collections exist is to provide a low memory collection if you know you are going to store 1 element and it is not going to be mutated. Especially in high volume services this can have significant impact due to garbage collection latency.

This applies for both Set.of("1"); and Collections.singleton("1");

Since Set is a Collection already returning the more constrained contract is a good thing for users of the libraries. You get additional functionality without paying anything for it.

And you as a user should do the same as for any other API and library, you should store the least needed contract for it.

So if the only thing you'll ever need to do with the structure is to iterate in a loop I'd suggest to choose Iterable instead of using List, Set or Collection. Since aCollection is an Iterable this will work out of the box.

java.util.Collections.singletonList() Method, I am struggling with something new I am attempting. The app is UWP. Most forms are master/detail. I need a global collection such that all forms will access the same instance of the data. There are no circumstances where the collection could be accessed simultaneously by more than one form or thread. I have a class (Model) named "Owner".

Set extends Collection. By returning Set you can use the return value where a Set or Collection is required. If Collection would be returned then you couldn't use the return value where a Set is required. Set is a special type of Collection that requires unique elements. Since the singleton Set contains only one element and will forever contain

Here, we created a List containing only one element. This is a convenient way than creating a list first, adding an element and then making it as unmodifiable. Note the lists created like this are immutable, which means making any changes to the list like add, remove or set will throw an Exception.

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.