Lemonio Lemonio - 5 months ago 37x
Java Question

Storing map in properties file

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
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


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


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.