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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:21:31+00:00 2026-06-03T11:21:31+00:00

I want to defined a generic parameter, which should extend Map or Collection, but

  • 0

I want to defined a generic parameter, which should extend Map or Collection, but I don’t know how to do it:

public <T> void test(T t) {}

I can write it as:

public <T extends Map> void test(T t) {}

or

public <T extends Collection> void test(T t) {}

But I don’t know is it possible to let T extend Map or Collection in a single method.

  • 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-03T11:21:34+00:00Added an answer on June 3, 2026 at 11:21 am

    Short answer: no.

    What do you intend to do with the t parameter within the method? Since Map and Collection have only Object as their common supertype, the only methods you can call on t will be those on Object. (Even methods on both interfaces, such as size(), will be rejected by the compiler.)

    With that in mind, is there any reason you can’t overload the method in this case? That is, define one implementation for each desired parameter type:

    public void test(Map<?,?> t) { ... }
    public void test(Collection<?> t) { ... }
    

    If you don’t want to do that for whatever reason, then it seems that you could equally just declare the method to take Object and perform a run-time type check of the class. This is semantically equivalent since you’d have to cast to call Map or Collection-specific methods anyway, though it does mean that the type check is done at compile time instead of runtime. Java’s type system doesn’t let you express this union-type dependency though, so I don’t see any other alternative.

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

Sidebar

Related Questions

I want to defined a variable/parameter in CSS and access it by javascript. What
I have defined a function in which i want to open a page on
I want create a generic (general) method which accepts the return types of LINQ
is there in C# some already defined generic container which can be used as
I have a generic class, on which I define a method that should take
I have a pre-defined database that I want to map using Entity Framework 4
I have custom control where i want to show some items. In generic.xaml defined
In asp.net mvc3 I want to have a generic method which adds a viewmodel
I want to load defined folders into my website from same system, now I
I want to check whether the endpoint is defined in the configuration before I'll

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.