• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

singletonXXX( ) methods in java.util.Collections

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all:
I am trying to understand the following methods in the java.util.Collections class:
Can you tell me under what circumstances does one use these methods and why would one use them?
static Set singleton(Object o)
Returns an immutable set containing only the specified object.
static List singletonList(Object o)
Returns an immutable list containing only the specified object.
static Map singletonMap(Object key, Object value)
Returns an immutable map, mapping only the specified key to the specified value.
Thanks in advance for the help.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they are just convenience methods, as stated in Sun's Collections tutorial.
So if you have a single object and you need to pass it to a method that takes a "java.util.List", this saves you from creating the List and adding the object to it. It's not a big savings [one statement instead of two], but so be it.
 
Pay attention! Tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic