1

Let's say I want to parse some JSON and I store the path in a Java constants class the path that I want to follow.

For example:

public static final List<String> path = Arrays.asList("a", "b", "c"); 

so that I can do this (pseudocodeish):

public boolean checkSomething() {
    JsonThing json = ...
    for (String path : Constants.path) {
        json.get(path);
    }

    return json.getAsString().equals("value");
}

Would it be better to store this constant as a String like:

public static final String path = "a:b:c";

and then do:

public boolean checkSomething() {
    JsonThing json = ...
    for (String path : Constants.path.split(":") {
        json.get(path);
    }
    return json.getAsString().equals("value");
}

Since the path is already stored in memory, would it be more efficient to just keep a : separated list since that String will take up less space than the array. In this instance, there's a little more work to be done (iterating over String to turn it into list), but then the list is garbage collected when the method is over.

  • 1
    "would it be more efficient" - from what you said so far, it looks like efficiency is not really a big enough of a concern, and, furthermore, looks like any hypothetical efficiency gain wouldn't have much of an impact, but you would still pay for it with this string that's relatively hard to read and maintain, and if that's the case, it's not really worth it. – Filip Milovanović Nov 23 '18 at 6:26
  • Perhaps you should edit the code example. It's quite hard to see how you're actually going to use the data from the example as it is now. What does the line json.get(path); do? It smells like a NOP. – COME FROM Nov 23 '18 at 7:25
  • 1
    Best Way to Store List as Constant ... the best way is to store it as a List of course. And the best way to store constants strings is as Strings, not an array of chars. The compiler and the JVM won't care too much (or not at all), but whoever has to read your code will. – Laiv Nov 23 '18 at 7:36
4

The difference in memory consumption of a List of Strings compared to a concatenated String should be meaningless. Choose the data structure that best suits the functional needs.

It's very likely a List object wrapping an array of Strings, which is what Arrays.asList("a", "b", "c") produces, is a good choice. A concatenated String, on the other hand, is rarely a good choice as a data structure.

However, if memory consumption is a real issue, which could be true if you need to hold millions of such paths, then you could consider alternatives. I have to say it's hard to imagine such case if your data consists of JSON paths. It smells like a serious design issue that should be solved on pen and paper before trying to fix it by optimizing at code level.

String itself is not an optimal way to store character data that makes uses of a very limited subset of Unicode. If I had to hold an enormous chunk of character data in memory, I would first look into encoding the character data efficiently instead of using Strings.

  • Good answer. One thing I will note though is these would be constants. They would never be concatenated to at run time. – flyinghigh Nov 24 '18 at 1:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.