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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:53:20+00:00 2026-06-09T19:53:20+00:00

I have a Map that contains key:value data for properties that are in different

  • 0

I have a Map that contains key:value data for properties that are in different properties files. I came to the following solution:

//firstProp - properties file
//FIRST_PROPERTIES_FILE - path to a properties file
public void changeProps(Map<String, String> props) {
    boolean isFirstPropsChanged = false;
    boolean isSecondPropsChanged = false;
    boolean isThirdPropsChanged = false;

    Set<String> keys = props.keySet();
    for(String key : keys) {
        if(firstProp.containsKey(key) && !firstProp.getProperty(key).equals(props.get(key))) {
            firstProp.setProperty(key, props.get(key));
            if(!isDirstPropsChanged) {
                isFirstPropsChanged = true;
            }
        }

        if(secondProp.containsKey(key) && !secondProp.getProperty(key).equals(props.get(key))) {
            secondProp.setProperty(key, props.get(key));
            if(!isSecondPropsChanged) {
                isSecondPropsChanged = true;
            }
        }

        if(thirdProp.containsKey(key) && !thirdProp.getProperty(key).equals(props.get(key))) {
            thirdProp.setProperty(key, props.get(key));
            if(!isThirdPropsChanged) {
                isThirdPropsChanged = true;
            }
        }
    }
    try {
        if(isFirstPropsChanged){
            firstProp.store(new FileOutputStream(FIRST_PROPERTIES_FILE), null);
        }
        if(isSecondPropsChanged) {
            secondProp.store(new FileOutputStream(SECOND_PROPERTIES_FILE), null);
        }
        if(isThirdPropsChanged) {
            thirdProp.store(new FileOutputStream(THIRD_PROPERTIES_FILE), null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This solution works well, but it looks like wheelchair. I looked through different DPs, Strategy seemed to be good here, but it only seemed… I even think that no DP should be applied and that there’s much delicate and simpler solution. But what it is in fact?

Updated
Refactoring according to Brian Agnew suggestion or smth. alike.

//Map<Properties, File> propertiesFileMap - Properties files with File they are located at.
public void changePropsArray(Map<String, String> props) {
    Set<String> keys = props.keySet();
    for(String key : keys) {
        for(Properties prop : propertiesFileMap.keySet()) {
            if(prop.containsKey(key) && !prop.getProperty(key).equals(props.get(key))) {
                prop.setProperty(key, props.get(key));
            }
        }
    }

    try {
        for(Map.Entry<Properties, File> entry : propertiesFileMap.entrySet()) {
            entry.getKey().store(new FileOutputStream(entry.getValue()), null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • 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-09T19:53:21+00:00Added an answer on June 9, 2026 at 7:53 pm

    What’s the matter with maintaining an array of Properties + their corresponding filename ? If you encapsulate these in an object then you simply have to loop through and that object will know how to update and write itself out.

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

Sidebar

Related Questions

I have HashMap object contains a key x-y-z with corresponding value test-test1-test2 . Map<String,String>
I have a Map<String,String> which contains a special value with a null key. It's
I have a service that contains a map: static Map cargosMap = ['1':'item1','2':'item 2','3':'item
I have a silverlight application that contains Bing map. I want when the user
I have a Map that uses a Set for the key type, like this:
I have a map that reads an XML file; it's all very simple and
I have a static map that is a private data member. How do I
I have a key value pair in Hashmap in Java, I am traversing Map
I have an XML file which contains multiple nodes that have children. If the
I have code that basically does the following to read in properties that 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.