• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What is the practical purpose of Collections.emptySet(Map/List)?

 
Ranch Hand
Posts: 122
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am cracking my head to find out the practical purpose of Collections.empty(Set|Map|List) when it returns an immutable Set/Map/List.

Could someone please clarify?

Thanks, Raghu
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It comes in handy if you want to assign an empty List , Set or Map as a default value, for instance to prevent returning a null reference.
You could just create a new instance of an appropriate implementation, but the emptyXXX() methods all return a reference to the same statically initialized immutable instance, which prevents unnecessary creation of objects.
 
Raghavendra Desoju
Ranch Hand
Posts: 122
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense !! Thanks Jelle !!
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic