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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:47:24+00:00 2026-06-12T13:47:24+00:00

I have a list of Foo and Bar objects, and a converter for each

  • 0

I have a list of Foo and Bar objects, and a converter for each of the corresponding ones.

public static void Convert(Foo1 a, Bar1 b) {
...
}
public static void Convert(Foo2 a, Bar2 b) {
...
}
etc

Some of the objects, however, contains lists.
The Convert-methods needs to be different, because Bar1 is vastly different from Bar2 and Bar3 and so on, but I want to create one method to handle all possible lists.

Is it possible to create a generic method that calls the appropriate non-generic method depending on the contents of the list?

So far I’ve tried this:

public static <T, S> void ConvertList(List<T> list, List<S> libList) {
    list = new ArrayList<T>(libList.size());

    for (int i = 0; i < libList.size(); ++i) {
        Convert(list.get(i), libList.get(i));
    }
}

But that doesn’t compile because “Cannot resolve method ‘Convert(T,S)'”

Any ideas?

  • 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-12T13:47:26+00:00Added an answer on June 12, 2026 at 1:47 pm

    No, you can’t do this – overloading is determined at compile-time, and it sounds like you want different methods to be called based on either the execution-time type of the objects involved, or at least on the execution-time type of T, which is completely unknown at execution time.

    Code-path decisions based on execution-time type are usually handled via overriding via overloading, but it’s not clear whether this is feasible in this case. If you could put an appropriate convert method in each Foo?? subclass, you could potentially constrain T and S accordingly, but basically there’s too much context we’re unaware of at the moment.

    You could use reflection to find and invoke the most appropriate method at execution time, but that’s going to be painful to code and potentially significantly slower.

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

Sidebar

Related Questions

I have a List of Foo Objects. If a name appears multiple times, I
I have a REST service that returns a list of objects. Each object contains
I have some Foo objects which are contained in Bar objects: class Foo {
I'm using Grails 1.3.7. Let's say I have the domain objects Foo and Bar
I have a list of objects and for each of them I want to
I have a class called Foo that defines a list of objects of type
I have a list of objects and each and every object in the list
I have a function that accepts a list of date objects and should output
class Foo { Bar b; List<Foo> Neighbours; } class Bar { Spam s; List<Bar>
Imagine I have a directory structure like so: parentDir\dirA\foo\ parentDir\dirB\foo\ parentDir\dirC\ parentDir\dirD\bar\foo\ parentDir\dirE\foo\ parentDir\dirF\

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.