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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:18:06+00:00 2026-06-14T02:18:06+00:00

Assigning a List<Object> to a List<? super String> works fine. Assigning a List<List<Object>> to

  • 0

Assigning a List<Object> to a List<? super String> works fine.

Assigning a List<List<Object>> to a List<List<? super String>> doesn’t compile.

Code

public class Main {
    public static void main(String[] args) {
        // works fine
        List<Object> listOfObject = new ArrayList<>();
        takeListSuperString(listOfObject);

        // doesn't compile
        List<List<String>> listOfListOfObject = new ArrayList<>();
        takeListOfListSuperString(listOfListOfObject);
    }

    static void takeListSuperString(List<? super String> listSuperString) {

    }

    static void takeListOfListSuperString(List<List<? super String>> listOfListSuperString) {

    }
}

Question

Why doesn’t List<List<? super String>> work the same as List<? super String>?

Also, any idea where I could look up things like this?

A related question is Generics hell: hamcrest matcher as a method parameter. But I don’t find the answers there helpful.

Edit

I had to think through JB Nizet’s answer for a couple of hours before I finally got it. So I’ll expand it a little bit here. Maybe that’ll help someone else.

Assuming that assigning a List<List<CharSequence>> to a List<List<? super String>> is possible, the following code would compile:

// can only contain instances of CharSequence
List<List<CharSequence>> listOfListOfCharSequences = new ArrayList<>();

List<List<? super String>> listOfListSuperString = listOfListOfCharSequences;

// add a list of objects containing an Integer
List<Object> listOfObjects = new ArrayList<>();
listOfObjects.add(123);
listOfListSuperString.add(listOfObjects);

// Ups.. exception at runtime we are getting an Integer where we expect a CharSequence
CharSequence charSequence = listOfListOfCharSequences.get(0).get(0);

So to prevent ugly exceptions at runtime it is not allowed.

As halex points out this is Generics covariance, same as a List<String> not being assignable to List<Object>. And with using List<? extends List<? super String>> the code would actually compile, because ? extends String prevents the List.add() call.

  • 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-14T02:18:07+00:00Added an answer on June 14, 2026 at 2:18 am

    Because a List<Object> is not the same thing as a List<? super String>. A List<? super String> holds objects of one specific type, that you don’t know, but which is String or a super-class or super-interface of String. Whereas a List<Object> is a List that can hold any kind of object.

    Suppose ? super String is CharSequence. Would you find it normal that the following compiles?

    List<List<Object>> listOfListOfObjects = new ArrayList<List<Object>>();
    List<Object> listOfObjects = new ArrayList<Object>(); 
    listOfObjects.add(new Integer());
    listOfListOfObjects.add(listOfObjects);
    List<List<CharSequence>> listOfListOfCharSequences = listOfListOfObjects; // WTF?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am assigning to an ImageView contacts images using this code: mPhotoView = (ImageView)
I'm assigning objects to this list of SqlParameter and then trying to execute the
Please can someone explain to me what this line of code does: var list
Button.Click += new RoutedEventHandler(_click); private void _click(object sender EventArgs e) { //... } In
First I am assigning a list values to Session like this HttpContext.Session[cont + schedule]
I have a class Foo defined like this: class Foo { public: Foo(int num);
I had a super noob question about assigning objects in objective-c. I basically am
Returning information from webservice. I have made a class for the return object. But
I have a list of objects, where each object has a boolean property named
I have a list of images that i’ve loaded with the Loader class, but

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.