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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:51:28+00:00 2026-05-16T16:51:28+00:00

While f1 does compile, the very similar f2 won’t and I just cant explain

  • 0

While f1 does compile, the very similar f2 won’t and I just cant explain why.
(Tested on Intellij 9 and Eclipse 3.6)

And really I thought I was done with that kind of question.

import java.util.*;
public class Demo {

    public  List<? extends Set<Integer>> f1(){
        final List<HashSet<Integer>> list = null;
        return list;         
    } 

    public  List<List<? extends Set<Integer>>> f2(){
        final List<List<HashSet<Integer>>> list = null;
        return list;         
    } 

}
  • 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-16T16:51:29+00:00Added an answer on May 16, 2026 at 4:51 pm

    List<List<HashSet<Integer>>> is not assignable to List<List<? extends Set<Integer>>> for the same reason List<HashSet<Integer>> would not be assignable to List<Set<Integer>>.

    You can get it to compile by changing this:

    public  List<List<? extends Set<Integer>>> f2(){
    

    into this:

    public  List<? extends List<? extends Set<Integer>>> f2(){
    

    The reason your code didn’t compile, and why the other example I gave (ie: “List<HashSet<Integer>> would not be assignable to List<Set<Integer>>“) is that Java generics are not covariant.

    The canonical example is that even if Circle extends Shape, List<Circle> does not extend List<Shape>. If it did, then List<Circle> would need to have an add(Shape) method that accepts Square objects, but obviously you don’t want to be able to add Square objects to a List<Circle>.

    When you use a wildcard, you’re getting a type that slices away certain methods. List<? extends Shape> retains the methods that return E, but it doesn’t have any of the methods that take E as a parameter. This means you still have the E get(int) method, but add(E) is gone. List<? extends Shape> is a super-type of List<Shape> as well as List<Circle>, List<? extends Circle>, etc. (? super wildcards slice the other way: methods that return values of the type parameter are removed)

    Your example is more complicated because it has nested type parameters, but it boils down to the same thing:

    • List<HashSet<Integer>> is a sub-type of List<? extends Set<Integer>>
    • Because generics are not covariant, wrapping the two types in a generic type (like List<...>) yields a pair of types that no longer have the sub/super-type relationship. That is, List<List<HashSet<Integer>>> is not a sub-type of List<List<? extends Set<Integer>>>
    • If instead of wrapping with List<...> you wrap with List<? extends ...> you’ll end up with the original relationship being preserved. (This is just a rule of thumb, but it probably covers 80% of the cases where you’d want to use wildcards.)

    Note that trashgod and BalusC are both correct in that you probably don’t want to be returning such a weird type. List<List<Set<Integer>>> would be a more normal return type to use. That should work fine as long as you’re consistent about always using the collection interfaces rather than the concrete collection classes as type parameters. eg: you can’t assign a List<ImmutableSet<Integer>> to a List<Set<Integer>>, but you can put ImmutableSet<Integer> instances into a List<Set<Integer>>, so never say List<ImmutableSet<Integer>>, say List<Set<Integer>>.

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

Sidebar

Related Questions

I am trying to get a very simple program to compile while using functions
While ruby does allow modifying any class at any place in the code, I
While it does not make much sense in the general case as it should
I am using IE8 in compatibility view and while this does produce acceptable rendering,
I'm having some difficulties creating a javascript appointment style calendar. While it does render,
When refactoring classes and methods JDT doesn't change referenced resources, while it does when
While running my application (which does a good bit of custom drawing) I get
Why does Google prepend while(1); to their (private) JSON responses? For example, here's a
Does set,bag while cascading is used for Ilist or Iset only. What if the
How does CouchDB handles conflicts while doing bi-directional replication? For example: Lets say there

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.