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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:54:38+00:00 2026-05-12T09:54:38+00:00

This may be a little subjective, but I have often found that it can

  • 0

This may be a little subjective, but I have often found that it can be very interesting to see how other developers approach certain daily details.

I have code which works like this:

class A {
    public List<SomeType> getOneSet() { ... }
    public List<SomeType> getAnotherSet() { ... }
}

class B {
    public static OtherType convert(SomeType input) { ... }
}

// ...

A a = new A();
List<OtherType> rgResults = new ArrayList<OtherType>();

And now would follow the equivalent of two identical for loops, like so:

for (SomeType input : a.getOneSet()) {
    rgResults.add(B.convert(input);
}

for (SomeType input : a.getAnotherSet()) {
    rgResults.add(B.convert(input);
}

This works, but it’s of course code duplication. If the code inside the loop gets a little more complicated or there’s more than two sets it’s not acceptable.

I therefore put the loop in a function that takes source and destination list as a parameter, but was curious to see if there are other ways. Especially ones that might be more appropriate when you’re never calling the function from more than one place.

For example I would have liked the following, which didn’t work because I can’t have arrays of generics:

for (List<SomeType> rgSrc : new List<SomeType>[] { a.getOneSet(), a.getAnotherSet() } ) {
    for (SomeType src : rgSrc) {
        rgResults.add(B.convert(src));
    }
}
  • 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-12T09:54:39+00:00Added an answer on May 12, 2026 at 9:54 am

    Try something like this:

    interface Getter<E, I> 
    {
      public E get(I item);
    }
    
    public static <T extends Collection<E>, I, E> T convert(T target, Collection<I> source, Getter<E, I> getter)
    {
      for (I item : source)
      {
        target.add(getter.get(item));
      }
    
      return target;
    }
    
    List<String> strings = CollectionUtil.convert(
      new ArrayList<String>(someItems.size)
      someItems,
      new Getter<String, MyClass>() 
      {
        public String get(MyClass item)
        {
          return item.toString();
        }
      }
    );
    

    You simply implement Getter as necessary depending on the converstion you want to do.

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

Sidebar

Related Questions

ok this may sound a little crazy but, i have class A and a
This may be a doozy, but does anyone have an idea how to: Pass
I know this is partially subjective, but hopefully with enough specifics I can get
Hopefully this question isn't as subjective as I think it may be. I have
This may be a little confusing but I having some trouble. My goal is
This may be little weird, but I want to persist only the scalar properties
So this may be a little tricky, but take a look at the div
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This may seem like a daft question, but i was wondering about how to
This may be simple one, but 5 mins of Googling didn't give me the

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.