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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:46:15+00:00 2026-05-23T14:46:15+00:00

How do I write a static method in Java that will take a List,

  • 0

How do I write a static method in Java that will take a List, perform an action on each element, and return the result (without affecting the original of course)?

For example, if I want to add 2 to each element what goes in the … here? The concrete return type must be the same, e.g. if my List is a LinkedList with values 1,2,3 I should get back a LinkedList with values 3,4,5. Similarly for ArrayList, Vector, Stack etc, which are all Lists.

I can see how to do this using multiple if (lst instanceof LinkedList) ... etc… any better way?

import java.util.List;

public class ListAdd {       

    static List<Integer> add2 (List<Integer> lst) {

    ...

        return result;
    }
}
  • 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-23T14:46:15+00:00Added an answer on May 23, 2026 at 2:46 pm

    OK well someone mentioned reflection. It seems to be an elegant solution:

    import java.util.*;
    public class ListAdd {
    
        static List<Integer> add2 (List<Integer> lst) throws Exception {
    
            List<Integer> result = lst.getClass().newInstance();
            for (Integer i : lst) result.add(i + 2);
    
            return result;
        }
    }
    

    Concise, but it thows an checked exception, which is not nice.

    Also, wouldn’t it be nicer if we could use the method on concrete types as well, e.g. if a is an ArrayList with values 1, 2, 3, we could call add2(a) and get an ArrayList back? So in an improved version, we could make the signature generic:

    static <T extends List<Integer>> T add2 (T lst) {
        T res;
        try {
            res = (T) lst.getClass().newInstance();
    
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    
        for (Integer i : lst) res.add(i + 2);
        return res;
    }    
    

    I think throwing a runtime exception is the least worst option if a list without a nullary construcor is passed in. I don’t see a way to ensure that it does. (Java 8 type annotations to the rescue maybe?) Returning null would be kind of useless.

    The downside of using this signature is that we can’t return an ArrayList etc as the default, as we could have done as an alternative to throwing an exception, since the return type is guaranteed to be the same type as that passed in. However, if the user actually wants an ArrayList (or some other default type) back, he can make an ArrayList copy and use the method on that.

    If anyone with API design experience reads this, I would be interested to know your thoughts on which is the preferable option: 1) returning a List that needs to be explicity cast back into the original type, but enabling a return of a different concrete type, or 2) ensuring the return type is the same (using generics), but risking exceptions if (for example) a singleton object without a nullary constructor is passed in?

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

Sidebar

Related Questions

I'm trying to write a method in java that will increment a counter for
I write a large static method that takes a generic as a parameter argument.
What's up y'all, I am trying to write some code in Java that will
I'm trying to write a method that will get a private field in a
I want to write an average method in java such that it can consume
Suppose I've got a method that accepts an array and processes each element in
I'm trying to write a method like this: public static T Test<T>() { if
I'd like to write an extension method for string, which appears as a static
I wrote a method that extracts fields from an object like this: private static
Is it possible to write the folowing using lambda(C#) private static void GetRecordList(List<CustomerInfo> lstCustinfo)

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.