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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:40:29+00:00 2026-06-13T11:40:29+00:00

The title is probably not the best, I apologize for that. I have several

  • 0

The title is probably not the best, I apologize for that.

I have several final static Lists I am using to define defaults for database values. The default list of values should never change, as such when populating them, I use Collections.nCopies(int,T) to obtain an immutable List. These Lists are then used to populate lists in another class with defaults. The values in these Lists are expected to change.

Pseudocode for the class of defaults:

public final class FooDefaults {
    public final static List<Integer> LIST_ONE;
    public final static List<String> LIST_TWO;
    //This map allows easier access to "column" values.
    public final static List<Map<String,String>> LIST_THREE;

    static {
        LIST_ONE = Collections.nCopies(7, 5);
        LIST_TWO = Collections.nCopies(10, "boo");
        Map<String, String> temp = new java.util.LinkedHashMap<>();
        for(int i=0;i<15;i++) {
            temp.put(("Param"+i),"foo");
        }
        LIST_THREE = Collections.nCopies(10, temp);
    }
}

Pseudocode for the class of editable values:

public class Foo {
    //Keep the reference from changing.
    //Prevents an accidental new.
    private final List<Integer> listOne;
    private final List<String> listTwo;
    private final List<Map<String,String>> listThree;

    public Foo() {
        listOne = new java.util.ArrayList<>(FooDefaults.listOne);
        listTwo = new java.util.ArrayList<>(FooDefaults.listTwo);
        listThree = new java.util.ArrayList<>(FooDefaults.listThree);
    }
}

My concern is that as I have performed a shallow copy on these lists, changes in the lists in Foo, will be visible in the Lists in FooDefaults.

This post: https://stackoverflow.com/a/1685158/1391956 suggests that due to Strings and Integers being immutable, I need not worry about accidentally overwriting the values in FooDefaults.LIST_ONE and FooDefaults.LIST_TWO.

Thus, my primary concern are the values contained within the maps in FooDefaults.LIST_THREE. If I change the values in the maps in Foo’s listThree, will the change be visible in FooDefaults?

If so, what would be the most efficient way to handle this? Class Foo is likely to be instantiated over a thousand times and added to a List in another class, thus speed will potentially be an issue.

I originally created the final static lists in FooDefaults in the interest of speed, as it is my (probably incorrect) assumption that creating those Lists in FooDefaults and simply copying the data would be faster than creating them every time Foo is instantiated.

EDIT: If I must perform a Deep Copy I plan on using something similar to:

public static final List<Map<String, String>> getListThreeCopy() {
    Map<String,String> temp = new java.util.LinkedHashMap<>();
    for(Map.Entry<String, String> entry: LIST_THREE.get(0).entrySet()) {
        temp.put(entry.getKey(),entry.getValue());
    }

    List<Map<String,String>> rtnList = new java.util.ArrayList<>();
    for(int i=0;i<LIST_THREE.size();i++) {
        rtnList.add(temp);
    }
    return rtnList;
}

Would there be a faster way?

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

    In the end, I used the code mentioned at the end of my post in a loop to perform a Deep Copy 1,500 times and timed it. I did the same with the instantiation code.

    As I somewhat expected, recreating the List from scratch is much faster than a deep copy.

    16 milliseconds with the Deep Copy versus 0 milliseconds with instantiation. I timed it using System.currentTimeMillis().

    So long as I only create it in a static function, I have little reason to worry about errors.

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

Sidebar

Related Questions

Okay that's probably not the best title, I know why we need browser resets:
Probably not the best title, but I'll explain: I have an array of objects
Alright, probably not the best title, but meh. I have the following code inside
As you can probably tell by the title, I'm not sure the best way
Probably not the best title I've ever written, but I find it hard to
The title is probably not very clear. I have the following example in mind:
Not sure if the title explains itself, but probably not! Anyway, I have 50
I'm probably not using the right words in the title since I'm new to
The title is probably not accurate but I hope that reading this post you
OK, the question title probably isn't the best, but I'm looking for a good

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.