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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:09:17+00:00 2026-05-18T08:09:17+00:00

Why does the declaration Set<Set<String>> var = new HashSet<Set<String>>(); work but the declaration Set<Set<String>>

  • 0

Why does the declaration

Set<Set<String>> var = new HashSet<Set<String>>();

work but the declaration

Set<Set<String>> var = new HashSet<HashSet<String>>();

choke?

I’m aware that ‘top level’ (not sure if that’s the correct phrase here) generics in a declaration play by different rules than those inside the pointy brackets, but I’m interested to learn the reason. Not an easy question to google, so I thought I’d try you guys.

  • 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-18T08:09:18+00:00Added an answer on May 18, 2026 at 8:09 am

    It’s because you could circumvent the type system if it were allowed. The property you desire is called covariance. If collections were covariant then you’d be able to do this:

    Set<Set<String>> var = new HashSet<HashSet<String>>();
    
    var.add(new TreeSet<String>());
    

    A TreeSet is a type of Set, and so static type checking would not prevent you from inserting a TreeSet into var. But var expects HashSets and HashSets only, not any old type of Set.

    Personally, I always write your first declaration:

    Set<Set<String>> var = new HashSet<Set<String>>();
    

    The outer class needs to have a conrete implementation, but there’s usually no need to nail down the inner class to HashSet specifically. If you create a HashSet of Sets you are good to go. Whether you then proceed to insert a series of HashSets into var is your choice later in the program, but no need to restrict the declaration.


    For what it’s worth, arrays in Java are covariant, unlike the collection classes. This code will compile and will throw a runtime exception instead of being caught at compile time.

    // Arrays are covariant, assignment is permitted.
    Object[] array = new String[] {"foo", "bar"};
    
    // Runtime exception, can't convert Integer to String.
    array[0] = new Integer(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A using declaration does not seem to work with an enum type: class Sample{
I have this code private static Set<String> myField; static { myField = new HashSet<String>();
Why does C#.Net allow the declaration of the string object to be case-insensitive? String
What does ::Base part mean in Person < ActiveRecord::Base class declaration? I'm new to
What I am trying to do is show a window, that does not explicitly
Does anyone know why using-declarations don't seem to work for importing type names from
Does anyone know how to get IntelliSense to work reliably when working in C/C++
I have a Makefile for a C program that has the declaration CC?=gcc Changing
EDIT : Re-written this question based on original answer The scala.collection.immutable.Set class is not
I have this delegate declaration: public delegate IEnumerable<T> SearchInputTextStrategy<T, U>(string param); Lets assume 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.