Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 148349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:56:34+00:00 2026-05-11T08:56:34+00:00

This is a follow up question to Is there a basic Java Set implementation

  • 0

This is a follow up question to ‘Is there a basic Java Set implementation that does not permit nulls?‘. (thank you to all of those who helped with the answer)

It seems that the way to do this, since Sun didn’t provide something simple like this OOTB, is with a wrapper / proxy. This seems nearly straight forward. What I am wondering is what are the pros/cons of the following two approaches of adding a collection, or is there another better approach?

Approach #1

public boolean addAll( Collection<? extends E> c) {     if ( null == c ) {         throw new NullPointerException( 'c cannot be null' );     }      /*      * This seems like a terrible abuse of exceptions when      * all I want to do is check the set for a null.      *       * However, simply running through each element of the      * Collection may provide much worse performance for large      * collections. And according to the Collection API, the      * contains method may throw NullPointerExceptions if the      * collection implementation does not allow null elements.      */     boolean collectionContainsNull = false;      try {         collectionContainsNull = c.contains(null);     } catch (NullPointerException safeToIgnore) {         /* Safe to ignore since we do not want nulls */     }      if (collectionContainsNull) {         throw new NullPointerException('c cannot contain null elements');     }      this.wrapperSet.addAll(c); } 

Approach #2

public boolean addAll( Collection<? extends E> c) {     if ( null == c ) {         throw new NullPointerException( 'c cannot be null' );     }       E[] a = ( E[] )c.toArray();       /*      * We have to iterate through the entire collection to check for      * a null. This won't take advantage of any optimizations that      * c.contains may be using.      *      * We don't call add(e) here because if we hit a null midway through      * we would violate the condition that the set remains unchanged      * on error.      */      for ( E e : a ) {          if (null == e) {               throw new NullPointerException('c cannot contain nulls');          }      }       this.wrapperSet.addAll(a); } 

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:56:35+00:00Added an answer on May 11, 2026 at 8:56 am

    The second approach is better. Never hide exceptions – you are relying on the assumption that c.contains(null) only throws a NullPointerException in the case that there is a null in the collection. However if the NullPointeException is thrown because of a problem with an equals method you will have a bug in your code – and you will have hidden it.

    Edit:

    From the JavaDoc for contains, NullPointerException is thrown – if the specified element is null and this collection does not permit null elements (optional).

    Given that it is an optional method you may wind up with an UnsupportedOperationException being thrown instead of the NullPointerExcepion (in addition to hiding the an error in equals).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 99k
  • Answers 99k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For methods like this, I prefer overloaded constructors. You're clearly… May 11, 2026 at 7:47 pm
  • Editorial Team
    Editorial Team added an answer Check the permissions on the eclipse folder in your application… May 11, 2026 at 7:47 pm
  • Editorial Team
    Editorial Team added an answer To some degree opening the "Javadoc" view will give you… May 11, 2026 at 7:47 pm

Related Questions

This is a follow-up question to Is there a difference between throw and throw
This is somewhat of a follow-up question to this question . Suppose I have
To quickly summarise my question: Is it feasible to programmatically change the name of
This is a follow-up to my question from yesterday . I have Scott Meyers'

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.