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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:35:37+00:00 2026-06-04T19:35:37+00:00

I am mainly a C# developer and I was teaching Data Structures to my

  • 0

I am mainly a C# developer and I was teaching Data Structures to my friend and they use Java in their University and I saw such an expression in Java:

void printCollection(Collection<?> c) {
    for (Object e : c) {
        System.out.println(e);
    }
}

I haven’t seen such a thing in C# so I wonder what’s the difference between Collection<T> and Collection<?> in Java?

void printCollection(Collection<T> c) {
    for (Object e : c) {
        System.out.println(e);
    }
}

I think it could have been written in the way above too. The guy in the documentation was comparing Collection<Object> and Collection<T> though.

Examples are taken from http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

  • 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-04T19:35:38+00:00Added an answer on June 4, 2026 at 7:35 pm

    Collection<?> is a collection of unknown type parameter.

    As far as the caller is concerned, there is no difference between

    void printCollection(Collection<?> c) { ... }
    

    and

    <T> void printCollection(Collection<T> c) { ... }
    

    However, the latter allows the implementation to refer to the collection’s type parameter and is therefore often preferred.

    The former syntax exists because it is not always possible to introduce a type parameter at the proper scope. For instance, consider:

    List<Set<?>> sets = new ArrayList<>();
    sets.add(new HashSet<String>());
    sets.add(new HashSet<Integer>());
    

    If I were to replace ? by some type parameter T, all sets in sets would be restricted to the same component type, i.e. I can no longer put sets having different element types into the same list, as evidenced by the following attempt:

    class C<T extends String> {
        List<Set<T>> sets = new ArrayList<>();
    
        public C() {
            sets.add(new HashSet<String>()); // does not compile
            sets.add(new HashSet<Integer>()); // does not compile
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being mainly a Java developer, I was a bit surprised by the result when
I'm a web developer currently using mainly JavaScript and HTML and I use various
I'm mainly a C# developer, but I'm currently working on a project in Python.
There is a list of projects here , mainly the Facebook Developer Toolkit and
I am mainly a business app developer and I hear terms like CQRS, ServiceBus,
As a .net developer mainly c#, I want to know what would be the
I'm a Java/C++ developer that never spent time in learning C# and the relative
I'm mainly a front-end developer but do a lot of CMS configuration stuff too.
I've been a java developer for most of my career, and developed and maintained
I'm right now a C#/Java developer, and I've been having a great time on

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.