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 863981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:24:03+00:00 2026-05-15T09:24:03+00:00

So, I have a class with a constructor like this: public FilterList(Set<Integer> labels) {

  • 0

So, I have a class with a constructor like this:

public FilterList(Set<Integer> labels) {
    ...
}

and I want to construct a new FilterList object with an empty set. Following Joshua Bloch’s advice in his book Effective Java, I don’t want to create a new object for the empty set; I’ll just use Collections.emptySet() instead:

FilterList emptyList = new FilterList(Collections.emptySet());

This gives me an error, complaining that java.util.Set<java.lang.Object> is not a java.util.Set<java.lang.Integer>. OK, how about this:

FilterList emptyList = new FilterList((Set<Integer>)Collections.emptySet());

This also gives me an error! Ok, how about this:

Set<Integer> empty = Collections.emptySet();
FilterList emptyList = new FilterList(empty);

Hey, it works! But why? After all, Java doesn’t have type inference, which is why you get an unchecked conversion warning if you do Set<Integer> foo = new TreeSet() instead of Set<Integer> foo = new TreeSet<Integer>(). But Set<Integer> empty = Collections.emptySet(); works without even a warning. Why is that?

  • 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-05-15T09:24:04+00:00Added an answer on May 15, 2026 at 9:24 am

    The short answer is – that’s a limitation of the type inference in Java’s generic system. It can infer generic types against concrete variables, but not against method parameters.

    I suspect this is because methods are dispatched dynamically depending on the runtime class of the owning object, so at compile time (when all generic information is resolved) you can’t actually know for sure what the class of the method parameter will be and hence can’t infer. Variable declarations are nice and constant, so you can.

    Someone else might be able to give more detail and/or a nice link. 🙂

    In any case, you can always specify the type parameters explicitly for generic calls like so:

    Collections.<Integer>emptySet();
    

    or even several parameters at once, e.g.

    Collections.<String, Boolean>emptyMap(); // Returns a Map<String, Boolean>
    

    This often looks a little cleaner than having to cast, in cases where inference doesn’t kick in.

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

Sidebar

Related Questions

I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
I have a varargs constructor like this : public class Sentence { public String[]
I have a class with a constructor that looks like this: public TimedWorker(int timerInterval,
I have a JInternalFrame class with a constructor that looks like this: public MyInternalFrame()
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
So say I have a class like this: class A { public: A( SomeHugeClass*
I have a class like this: public class BaseClass { public BaseClass(URL url, String
Good afternoon all, I have a class which looks like this: public class Grapheme
I want to do something like this: @Stateless public class GreeterEjb { private final
I have this class constructor: public Category(int max){ ... } The thing is, I

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.