0

I have the following structure:

import java.util.LinkedHashMap;
...
LinkedHashMap <String, Object>level0 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level1 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level2 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level3 = new LinkedHashMap<String, Object>();

level1.put("level2", level2);
level2.put("level2", level3);

level0.put("level1", level1);

System.out.println(level0);

Output this:

{
    level1={
        level2={}
    }
}

I need to set a value through a "path" (or something), would be something like this:

MapThisObject example = new MapThisObject(level0);
example.putValue("level1.level2", "string", "test");
example.putValue("level1.level2", "int", 1);
example.putValue("level1.level2", "object", new LinkedHashMap());

System.out.println(example.result());
/*output:
{
    level1={
        level2={
            string="test",
            int=1,
            Object={}
        }
    }
}
*/

In other words, there is the possibility to put or set values ​​for "multidimensional objects" through a "path" (like Xpath)?

Protomen
  • 9,471
  • 9
  • 57
  • 124
  • What is your question? Is it; can you nest maps? What happened when you tried? I suggest you use generics as it can make you life easier. – Peter Lawrey Jan 27 '14 at 19:15
  • @PeterLawrey thanks for the suggestion, but what I want is something like **jxpath** ( http://commons.apache.org/proper/commons-jxpath/ ) only instead of getting the values ​​I need to *put* or *set*. – Protomen Jan 27 '14 at 19:20
  • You can do that. You don't even have to use Maps, with reflection, you can do this with objects. – Peter Lawrey Jan 27 '14 at 19:24
  • @PeterLawrey are you suggesting I do this manually? – Protomen Jan 27 '14 at 19:25
  • I suggest you write a simple method to do it manually. – Peter Lawrey Jan 27 '14 at 19:27
  • manually is unfeasible. check my idea: I get the variable `level0`, create a routine using the loops (while/for) he will convey all other copying again to a temporary variable. When he find a place that should add the new value existed a condition to do this and will add temporary variable. When you finish the loop, that copies the entire "level0" variable A to variable temporarily I just do this: `level0 = tmp;`. Would that be? – Protomen Jan 27 '14 at 19:39

2 Answers2

2

A simple example

public static void set(Map<String, Object> map, String path, Object value) {
    String[] parts = path.split("\\.");
    for(int i = 0; i < parts.length-1 ; i++) {
        String key = parts[i];
        Map<String, Object> map2 = (Map<String, Object>) map.get(key);
        if (map2 == null) {
            map.put(key, map2 = new LinkedHashMap<String, Object>());
        }
        map = map2;
    }
    map.put(parts[parts.length - 1], value);
}

set(example, "level1.level2.string", "test");
set(example, "level1.level2.int", 1);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • It worked perfectly, only had to change some things in your code and mine too. My problem is that I think I missed something basic in java, I did not know that capturing sub-variables caused them to still stay connected with the variable "level0". Thanks! – Protomen Jan 27 '14 at 19:53
0

From what you've described, it sounds like all you need is a map containing maps, nested to however many axes you're trying to select from.

The alternative would be to build your own tree structure, of course. Or to express it as an XML DOM tree, which would let you use standard XPath.

keshlam
  • 7,931
  • 2
  • 19
  • 33
  • I know I'll have to develop the structure. I wonder how I build it. I need to change the variable "level0" she has no default. I create loops(while/for) and go level by level by saving the data to a new object, until the function finds the location that should be added to the new value, and then end the routine and the new object will replace the "level0"? – Protomen Jan 27 '14 at 19:30
  • What's wrong with the nested maps solution? (And if you're asking us to educate you in how to build and maintain tree structures, I'd recommend that you go looking for a basic programming class.) – keshlam Jan 27 '14 at 19:32
  • My problem is that I think I missed something basic in java, I did not know that capturing "sub-variables" caused them to still stay "connected" with the variable "level0". – Protomen Jan 27 '14 at 19:58
  • OK, spelling it out a bit more: If you insert map into maps as values, you can use the first key and the first map to select the second map, which you can then use with the second key to select a third map (or the object, depending on how deep you need to go). This isn't anything special about Java; it's data structure design. – keshlam Jan 27 '14 at 20:03
  • True, this went unnoticed by me or really what you believe to be basic may not be so evident. But thanks for the help. – Protomen Jan 27 '14 at 20:05