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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:19:10+00:00 2026-06-10T11:19:10+00:00

Given a List instance, what is the most efficient way of increasing the size

  • 0

Given a List instance, what is the most efficient way of increasing the size of the List by a factor of f such that the new elements are duplicates of the original elements, interleaved with the original array?

e.g.

f        = 2
Original = [a,b,c,...,x,y,z]
New      = [a,a,b,b,c,c,...,x,x,y,y,z,z]

My current implementation is this:

List< Foo > interleave( List< Foo > original, int f ) {
    int newSize = original.size() * f;
    List< Foo > interleaved = new ArrayList< Foo >( newSize );

    for( Foo foo : original ) {
        for( int j = 0; j < factor; j++ ) {
            interleaved.add( new Foo( foo ) );
        }
    }
}

The problem is that my original list can be quite large, so performance isn’t very good. I have a hunch that there is a much more efficient way to do this; does anyone have any suggestions?

  • 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-10T11:19:11+00:00Added an answer on June 10, 2026 at 11:19 am

    The code you provide is well optimized, but there are some improvements that can be made; The significant ones depending on your exact needs.


    First, if your cloned elements are going to stay with the same value as the original, or otherwise just a few of them (compared to the total) are going to have their values changed, you might want to consider a reference-based cloning instead of the current “true-clone it all” code, if not a completely different approach that doesn’t even create new lists.

        /**
         * PROS:
         * -Very low memory-footprint, as no new objects are created in memory, just references to a single (original) object.
         * -Can be done with generalization; A single method will function for most classes and data-types, as is below.
         * 
         * CONS:
         * -If you need each clone element to be changed independently from eachother and/or the orininal, this will not work directly,
         * because any change to an reference-element will apply to all other reference-elements that point to that same Object.
         * 
         * @param <E> Sub-class generalizator. Used so that the returned list has the same sub-class as the source.
         * @param list Source list. The list containing the elements to be interleaved.
         * @param f The factor to interleave for. In effect, the number of resulting elements for each original.
         * @return A list containing the interleaved elements, with each element being a REFERENCE to the original object.
         */
        public static <E> List<E> interleaveByReference(List<E> list, int f) {
            List<E> interleaved = new ArrayList<E>(list.size() * f);
            for (E obj : list) {
                for (int i = 0; i < f; i++) {
                    interleaved.add(obj);
                }
            }
            return interleaved;
        }
    

    In case you are going to need just a few of your clones to change value, it might be better for your interleaved list to be reference-based, and the elements that need to be changed to be replaced individually later on.

    Note however, that the effectiveness of this approach will be highly dependent on how much of your original list’s elements will need to be changed; And that if too many need changes, this method, although still better in memory-footprint, will be worse in speed performance (which seems to be your main concern).

    The “later on individual cloning” can be achieved with something similar to this:

    public static void replaceWithTrueClone(List<String> list, int objIndex) {
        list.add(objIndex, new String(list.get(objIndex)));
        list.remove(objIndex + 1);
    }
    
    //OR
    
    public static void replaceWithNewObject (List<String> list, int objIndex, String newObject) {
        list.add(objIndex, newObject);
        list.remove(objIndex + 1);
    }
    

    If most of every element is going to have independent values in the course of your program’s execution, then your current method is pretty accurate already.

    There are two improvements that can be made. It will be easier to show it in the code directly, so that’s what I’ll do:

        /**
         * PROS:
         * -Each element is an independent object, and can be set to independent values without much of an effort.
         * 
         * CONS:
         * -Each element has it's own allocated memory for it's values, thus having a much heavier memory footprint.
         * -Is constructor-dependent, and thus cannot be generalized as easily;
         * Each different expected class will probably need it's own method.
         * 
         * @param list Source list. The list containing the elements to be interleaved.
         * @param f The factor to interleave for. In effect, the number of resulting elements for each original.
         * @return A list containing the interleaved elements.
         * For each of the original elements, the first is a REFERENCE, and the other are CLONES.
         */
        public static List<String> interleaveByClone(List<String> list, int f) {
            List<String> interleaved = new ArrayList<String>(list.size() * f);
            for (String obj : list) {
                interleaved.add(obj); //The first element doesn't have to be cloned, I assume.
                //If it has to be cloned, delete the line above, and change 'i=1' to 'i=0' on the line below.
                for (int i = 1; i < f; i++) {
                    interleaved.add(new String(obj));
                }
            }
            return interleaved;
        }
    
        /*
         * What was changed from the original is commented below.
         */
    
        public static List<String> original(List<String> original, int factor) {
            /*
             * It is unnessessary to have this 'newSize' variable. It gets needlessly maintained until the end of the method.
             * Although the impact is unworthy of measurement (negligible), it still exists.
             */
            int newSize = original.size() * factor;
            List<String> interleaved = new ArrayList<String>(newSize); //Just do the '*factor' operarion directly, instead of 'newSize'.
    
            for (String foo : original) {
                /*
                 * If you can use the original here, that's one less cloning operation (memory-allocation, etc...) per original element.
                 * A low-impact optimization, but still a good one.
                 */
                for (int j = 0; j < factor; j++) {
                    interleaved.add(new String(foo));
                }
            }
            return interleaved;
        }
    

    With an original list of two million elements, and a factor of 2, I get the following average speeds over 10 runs:

    • It takes 6030 (~) millis to create and fill the original list with
      2000000 different elements.
    • It takes 75 (~) millis to interleave the list with the
      interleaveByReference() method.
    • It takes 185 (~) millis to interleave the list with the
      interleaveByClone() method.
    • It takes 210 (~) millis to interleave the list with the
      original() method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of elements like so: int[] ia = new int[] { -4,
The original code was somehow complex, i simplify it as: Given: list of class
Given a List such as List(1, 2, 3, 4, 5, 6, 7) what is
Given a list of potential ID's is there a quick way using a single
I'm looking for an efficient way to calculate the rank vector of a list
Given a SQLAlchemy mapped class Table and an instance of that class t ,
I am looking for a method that reverses the same instance of a given
Given an R list, I wish to find the index of a given list
Given a list a containing vectors of unequal length and a vector b containing
Given a list like this: num = [1, 2, 3, 4, 5] There are

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.