Skip to content

Instantly share code, notes, and snippets.

@kdrakon
Last active November 17, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdrakon/0927411274a770147f9f to your computer and use it in GitHub Desktop.
Save kdrakon/0927411274a770147f9f to your computer and use it in GitHub Desktop.
I needed to re-create an ArrayListMultiMap from some real-world data to test something locally, but I only had access remotely via the toString method
class GuavaArrayListMultiMapFromToString {
static ArrayListMultimap<String, String> toArrayListMultimap(final String toString)
{
final ArrayListMultimap<String, String> map = ArrayListMultimap.create();
final Pattern pattern = Pattern.compile("\\w+=\\[.+?\\]");
final Matcher matcher = pattern.matcher(data);
while(matcher.find())
{
final String group = matcher.group();
final String[] split = group.split("=", 2);
final String key = split[0];
final String valueStr = split[1].replaceAll("[\\[|\\]]", "");
final List<String> values = Splitter.on(",").trimResults().splitToList(valueStr);
map.putAll(key, values);
}
return map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment