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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:31:59+00:00 2026-05-23T11:31:59+00:00

The java HashSet implementation has a constructor: public HashSet(Collection<? extends E> c) { map

  • 0

The java HashSet implementation has a constructor:

public HashSet(Collection<? extends E> c) {
    map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));
    addAll(c);
} 

Why it is Collection<? extends E> c? This isn’t enough: Collection<E> c?

  • 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-23T11:31:59+00:00Added an answer on May 23, 2026 at 11:31 am

    The concept here is called variance (covariance, contravariance).

    Let’s say you have the following two classes:

    class A {}
    class B extends A {}
    

    In this case, you can say that an instance of B is an instance of A. In other words, the following code is perfectly valid:

    A instance = new B();
    

    Now, generic classes in Java are, by default, invariant. That means that a List<B> is not a List<A>. In other words, the following code will not compile:

    List<A> as = new ArrayList<B>(); // error - Type mismatch!
    

    However, if you have an instance of B, sure you can add it to a list of A (because B extends A):

    List<A> as = new ArrayList<A>();
    as.add(new B());
    

    Now, let’s say you have a method that deals with lists of A by consuming its instances:

    void printAs(List<A> as) { ... }
    

    It would be tempting to make the following call:

    List<B> bs = new ArrayList<B>();
    printAs(bs); // error!
    

    However, it won’t compile! If you want to make such a call work, you have to make sure that the argument, List<B>, is a subtype of the type expected by the method. This is done by using covariance:

    void printAs2(List<? extends A> as) { ... }
    List<B> bs = new ArrayList<B>();
    printAs2(bs);
    

    Now, this method takes an instance of List<? extends A>, and it is true that List<B> extends List<? extends A>, because B extends A. This is the concept of covariance.


    After this introduction, we can go back to the constructor of HashSet you mention:

    public HashSet(Collection<? extends E> c) { ... }
    

    What this means is that the following code will work:

    HashSet<B> bs = new HashSet<B>();
    HashSet<A> as = new HashSet<A>(bs);
    

    It works because HashSet<B> is a HashSet<? extends A>.

    If the constructor were declared as HashSet(Collection<E> c), then the second line on the wouldn’t compile, because, even if HashSet<E> extends Collection<E>, it is not true that HashSet<B> extends HashSet<A> (invariace).

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

Sidebar

Related Questions

I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet .
Does anyone know how Java implements its hash tables (HashSet or HashMap)? Given the
If every object added to a java.util.HashSet implements Object.equals() and Object.hashCode() in a deterministic
HashSet is based on HashMap. If we look at HashSet<E> implementation, everything is been
Newbie question about java HashSet Set<User> s = new HashSet<User>(); User u = new
I have the following example: package cage; import java.util.HashSet; import java.util.Set; import animals.Animal; public
The interface for service layer is : EMS.java: public interface EMS extends UserDetailsService {
So, if I try to remove elements from a Java HashSet while iterating, I
Java has generics and C++ provides a very strong programming model with template s.
Java has a convenient split method: String str = The quick brown fox; String[]

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.