Storing map in properties file

40,041

Solution 1

Since you've indicated you don't want to use JSON, you could store the map as a single property like this:

map=key1=value1,key2=value2,key3=value3

Use Guava's Splitter and Joiner to simplify reading and writing the map:

String formatMap(Map<String, String> map) {
    return Joiner.on(",").withKeyValueSeparator("=").join(map);
}

Map<String, String> parseMap(String formattedMap) {
    return Splitter.on(",").withKeyValueSeparator("=").split(formattedMap);
}

This will work so long as the keys and values do not contain "," or "=" characters.

Solution 2

Format the contents of the Map as a String. Use a standard format like JSON. With the json-smart library this would look something like:

JSONObject json = new JSONObject();
json.putAll(WordMap);
String serializedMap = json.toString();
prop.setProperty("wordMap", serializedMap);

To parse the map:

String serializedMap = prop.getProperty("wordMap");
JSONObject json = (JSONObject) JSONValue.parse(serializedMap);
Map<String, String> wordMap = new HashMap<>();

for (Map.Entry<String,Object> entry : json.entrySet()) {
    wordMap.put(entry.getKey(), (String) entry.getValue());
}
Share:
40,041

Related videos on Youtube

LemonMan
Author by

LemonMan

Updated on October 13, 2020

Comments

  • LemonMan
    LemonMan over 3 years

    I know that I can build a Map as below.

    private static final ImmutableMap<String,String> WordMap = 
    ImmutableMap.<String, String>builder()
    .put("blah", "blahblah").put("blabla", "blahblahblah").build()
    

    I'd like to store the values of my map in a config file. I'm already storing the values for a different hashset in the config file by doing values=value1,value2,value3 and then
    new HashSet<String>(Arrays.asList(prop.getProperty(values).split(",")))

    I'd like to do something similar for my map. Any tips? I'm using java.util.Properties

  • Sotirios Delimanolis
    Sotirios Delimanolis almost 11 years
    You may want to also change that it doesn't use internally. I understand that as it has an instance variable. It actually extends Hashtable. It may confuse others.
  • Juned Ahsan
    Juned Ahsan almost 11 years
    @Zim-ZamO'Pootertoot public class Properties extends Hashtable<Object,Object>
  • LemonMan
    LemonMan almost 11 years
    i can? how? do i just write map=(key,value),(key,value) etc... ?
  • LemonMan
    LemonMan almost 11 years
    Alright, thanks! does the map have to be created with that code? is properties more than just a text file? because the idea was people would be able to configure the contents of the map within the text file, if they need to run code to do it there's no point in including it as they might as well change the put statements in the code
  • dnault
    dnault almost 11 years
    The idea behind this answer was that if you want to store the word map in a single property, you'll need to format it as a string. JSON is a standard format with good library support, but since you intend for end-users to edit the properties file JSON probably isn't the best choice. Just use whatever format you think will be simplest for your users, and write custom code to format and parse it. map=key1=value1,key2=value2,key3=value4 would work fine, as long as your keys and values can never contain "=" or "," characters.
  • LemonMan
    LemonMan almost 11 years
    thanks! so i could just get the property, call parseMap and set it equal to my map
  • LemonMan
    LemonMan over 10 years
    would there be an efficient way to do the second statement with a reverse order? if i want a value -> key map as well and don't want to go through the entire thing and recreate a new one or store a second configuration in the properties file
  • dnault
    dnault over 9 years
  • dnault
    dnault almost 8 years
    After posting this answer, Jackson has become my library of choice for JSON [de]serialization and data binding.