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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:06:32+00:00 2026-05-19T14:06:32+00:00

I’m writing a method in Java: List<Foo> computeFooList(/* arguments */) { /* snip */

  • 0

I’m writing a method in Java:

List<Foo> computeFooList(/* arguments */)
{
    /* snip */
}

I’d like to write a second method with exactly the same logic, but a different return type:

List<String> computeStringList(/* same arguments */)
{
    /* snip */
}

I’m trying to figure out a non-hackish way to minimize the amount of repeated code between the two methods. The only logical difference between the two is that, when adding an object to the list that’s returned, the first method adds the acutal Foo:

List<Foo> computeFooList(/* arguments */)
{
    List<Foo> toReturn = ...
    ...
    for (Foo foo : /* some other list of Foo */)
    {
        if (/* some condition */)
        {
            toReturn.add(foo);
        }
    }
    ...
    return toReturn;
}

and the second adds a String representation of the Foo:

List<String> computeStringList(/* same arguments */)
{
    List<String> toReturn = ...
    ...
    for (Foo foo : /* some other list of Foo */)
    {
        if (/* some condition */)
        {
            toReturn.add(foo.toString());
        }
    }
    ...
}

In reality, it’s not quite that simple. I don’t want to add a Foo to toReturn unless I’m absolutely sure it belongs there. As a result, that decision is made per-foo using helper functions. With two different versions of the methods, I’d need different versions of the helper functions as well – in the end, I’d be writing two sets of nigh-identical methods, but for one little generic type.


Can I write a single method which contains all of the decision-making logic, but can generate either a List<Foo> or a List<String>? Is it possible to do this without using raw List types (bad practice in generics land!) or wildcard List<?> types? I imagine an implementation that looks something like this:

List<Foo> computeFooList(/* args */)
{
    return computeEitherList(/* args */, Foo.class);
}

List<String> computeStringList(/* args */)
{
    return computeEitherList(/* args */, String.class);
}

private List<???> computeEitherList(/* args */, Class<?> whichType)
{
    /* snip */
}

Is there any nice, elegant way to do this? I’ve been playing around with generic methods, but I can’t see a way to do this. Even mucking about with reflection hasn’t gotten me anywhere (maybe I need something like TypeToken? …eww).

  • 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-19T14:06:33+00:00Added an answer on May 19, 2026 at 2:06 pm

    Can’t you externalize transformation logic into a separate strategy (such as Guava’s Function<F, T>):

    public <T> List<T> computeList(/* arguments */, Function<? super Foo, T> f) {
         List<T> toReturn = ...     ...     
         for (Foo foo : /* some other list of Foo */) {
             if (/* some condition */) {
                 toReturn.add(f.apply(foo));
             }
         }
         return toReturn;
    } 
    

    computeFooList:

    computeList(..., Functions.identity());
    

    computeStringList:

    computeList(..., Functions.toStringFunction());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.