• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 117
2
  • Mark post as helpful
  • send pies
  • 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
  • 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: 117
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense !! Thanks Jelle !!
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic