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

  • SEARCH
  • Home
  • 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 8922317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:48:09+00:00 2026-06-15T06:48:09+00:00

First of all, here’s the behavior added by Java 7’s Collections.checked* : Since null

  • 0

First of all, here’s the behavior added by Java 7’s Collections.checked*:

Since null is considered to be a value of any reference type, the returned collection permits insertion of null elements whenever the backing collection does.

This doesn’t appear to be listed in the compatibility documentation, though. Demo:

public class MyAPI {
    private Set<Polygon> polygons = new Collections.checkedSet(new HashSet<Polygon>(), Polygon.class);
    public Set<Polygon> getPolygons() {
        return polygons;
    }
}
public class MyAPITest {
    // This JUnit test passes when using Java 6 or earlier, but fails for Java 7.
    @Test(expected=NullPointerException.class)
    public void testAddNullPolygon() {
        new MyAPI().getPolygons().add(null);
    }
}

So as you can see I’m writing an API that exposes a Set for client code to populate. From what I’ve read, this is one of the use cases for Collections.checkedCollection etc: the added runtime check helps prevent weird stuff from getting inserted.

I’ve changed my API to handle nulls regardless, but my concern is that the client code could sometimes throw the NPE, sometimes not, depending on what version of Java the end user was running. That just feels broken. Ideally I’d like to preserve the old behavior, preventing nulls at insertion time.

I guess my options are:

  1. Give up on runtime checking entirely.

  2. Not worry about it, and trust client code to never insert nulls.

  3. Declare that my API only supports JRE 7.

  4. Use Guava, which looks great but is an extra dependency my API would be saddled with.

  5. Roll my own Set wrapper that enforces null and type checking.

  6. Some other more elegant solution that I’m missing.

Any guidance would be much appreciated!

  • 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. Editorial Team
    Editorial Team
    2026-06-15T06:48:10+00:00Added an answer on June 15, 2026 at 6:48 am

    The option that I ultimately went with is to get rid of the checkedCollection, making the exposed collection unmodifiable. Users of the API need to call one of the extra add/remove/clear methods to modify the collection.

    This limits API users somewhat: they can’t, for example, use addAll to copy all elements of another collection in a single method call. But it’s a fair tradeoff for simplicity and type safety.

    public class MyAPI {
        private Set<Polygon> polygons = new HashSet<Polygon>();
        private Set<Polygon> polygonsReadonlyView = Collections.unmodifiableSet(polygons);
        public Set<Polygon> getPolygons() {
            return polygonsReadonlyView;
        }
        public boolean addPolygon(Polygon p) {
            if (p == null) {
                throw new IllegalArgumentException("polygon cannot be null");
            }
            return polygons.add(p);
        }
        public boolean removePolygon(Polygon p) {
            return polygons.remove(p);
        }
        public void clearPolygons() {
            polygons.clear();
        }
    }
    

    One takeaway from all this is to never rely on a checkedCollection to prevent nulls from being inserted.

    A Google search might suggest this… but don’t do it! Null-checking is not one of checkedCollection’s use cases.

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

Sidebar

Related Questions

First of all I know similar questions has been asked here, I've checked but
First of all i'm new in here and new with csharp. just exhausted while
First of all I've gone through dozens of posting here on SO and google
first of all this is my third question about web services here and i
First of all, I could not decide if I should ask this here or
First of all, I know there are a few quite similar questions here on
First of all, I do not believe this belongs on Superuser. This belongs here
um, first post here, this place seems to be all over google and i
First of all here are some code snippets: public void startThread() { this.animationThread =
First of all have a look at here, www.zedge.net/txts/4519/ this page has so many

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.