Last active
November 17, 2015 14:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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