Skip to content

Instantly share code, notes, and snippets.

@pauldambra
Last active August 18, 2018 08:45
Show Gist options
  • Save pauldambra/a96bd0839464a596dfc373fc85adbbde to your computer and use it in GitHub Desktop.
Save pauldambra/a96bd0839464a596dfc373fc85adbbde to your computer and use it in GitHub Desktop.
Beautiful
private static final Splitter newlineSplitter = Splitter.on(Pattern.compile("\r?\n"));
private static final Splitter spaceSplitter = Splitter.on(CharMatcher.whitespace()).omitEmptyStrings();
Map<String, String> substitutions = newlineSplitter
.splitToList(text)
.stream()
.map(spaceSplitter::splitToList)
.filter(r -> r.size() != 0)
.collect(Collectors.toMap(
ss -> ss.get(0),
ss -> ss.get(1)
));
val substitutions = text.split(Pattern.compile("\r?\n"))
.map { it.split(' ') }
.filter { it.isNotEmpty() }
.associate { it[0] to it[1] }
@pauldambra
Copy link
Author

Takes input

123 456
789 101112

and converts to a map

{
123: 456,
789: 101112
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment