![]() ![]() Note Arrays.asList creates a fixed size list while Collections.singletonList creates an immutable list and therefore does not allow operations like set (int index, E element). (It takes roughly zero imagination to think of one.)īut I don't know how you would (objectively) distinguish between an ordinary use-case and a "specially useful" one. Use Collections Singleton List Description Replaces Arrays.asList with 0 or 1 parameters respectively with Collections.emptyList () or Collections.singletonList (.). String javaSrcDisplayName String.format(s Java source, displayName). public static List singletonList (T o) Parameters o This is the sole object to be stored in the returned list.Declaration Following is the declaration for () method. Why would I want to have a separate method to. Indeed, any program where you need to use an immutable list with one element is a valid use-case. The singletonList (T) method is used to return an immutable list containing only the specified object. What is the use of Collections.singletonList() in Java I understand that it returns a list with one element. ![]() It is part of the specification of the method see above.Īre there any special useful use-cases for this method, rather than just being a convenience method?Ĭlearly, there are use-cases where it is convenient to use the singletonList method. Following is the declaration for java.util. (An application that creates vast numbers of singleton lists is unusual to say the least.) The singletonList(T) method is used to return an immutable list containing only the specified object. It may also be a bit faster and/or save a bit of memory, but it is unlikely that these small savings will be significant. ![]() to save you having to write a sequence of statements to: Why would I want to have a separate method to do that? "Returns an immutable list containing only the specified object. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |