![]() ![]() (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. List list Collections.The list created with this method is also immutable, so you are sure there will not be any more elements in the list, at any condition. Indeed, any program where you need to use an immutable list with one element is a valid use-case. Using Collections.singletonList () This is the simplest and recommended method to create an immutable List with a single element inside it. CollectionsSingletonList, new JsonReader. When we want to initialize a fixed-size list with only one single element, we can consider using the Collections.singletonList() method. This java examples will help you to understand the usage of. It contains polymorphic algorithms that operate on collections. In this quick article, we've discussed the Arrays.asList() method and the Collections.singletonList() method. singleton, singletonList, and singletonMap - Return an immutable singleton set, list, or map, containing only the specified object (or key-value mapping). 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. This class consists exclusively of static methods that operate on or return collections. ![]() (An application that creates vast numbers of singleton lists is unusual to say the least.) 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |