• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set that prohibits null values  RSS feed

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been looking through the implementations of the Set interface available in JDK 5 and can't find any that prohibits null values. Does such an implementation exist?

Thanks,
Dan
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says in the Set interface API documentation that a Set contains at most one null. It says some implementations prohibit nulls, but not which.
Suggest, maybe, you write a wrapper class around a HashSet and for all the add methods throw an Exception (probably NullPointerException) if the element is null.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could take the one from apache's commons collections:
http://commons.apache.org/collections/

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. I should have mentioned that I'm looking for a generified Set that prohibits nulls, so given that extra requirement, the Apache commons implementation is unsuitable.

Cheers,
Dan
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about making it yourself? Is this what you're looking for?

[ June 26, 2008: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!