Merging Hierarchical Paths in Java

I’ve been doing heavy refactoring over the last 48 hours to cope with epic VMware fail. One part of that has been the necessity to do something pretty tricky, and as it turns out, Apache Commons has a massive shortcut I thought I’d share with the rest of you.

Imagine there are multiple tree branch paths and let KX equal the terminating value:


I needed to merge these paths so that I ended up with one hierarchy:

    |-- K2

I knew how to do this merge with linear performance, but I was trying to come up with a solution that was more optimal. It turns out Apache already has one, and it’s called Hierarchical Configuration. Basically you just do the following:

import org.apache.commons.configuration.HierarchicalConfiguration;

HierarchicalConfiguration hc = new HierarchicalConfiguration();
hc.addProperty(“A.B.C.D”, “K1”);
hc.addProperty(“A.B”, “K2”);
hc.addProperty(“D”, “K3”);

The object will create new branches if the paths do not yet exist, but if they do, the values will simply be inserted into the appropriate location. This has been a massive timesaver and hopefully you all will find it useful as well.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s