Skip to content

Instantly share code, notes, and snippets.

@alex-popov-tech
Last active June 12, 2017 19:12
Show Gist options
  • Save alex-popov-tech/6d07f4d4e6d2b866047e1af831fbde15 to your computer and use it in GitHub Desktop.
Save alex-popov-tech/6d07f4d4e6d2b866047e1af831fbde15 to your computer and use it in GitHub Desktop.
generateJsonForOptions
вариант 0
private String generateJsonForOptions(final String... values) {
return "{" + IntStream.range(0, values.length / 2)
.boxed()
.map(i -> String.format("\\\"%s\\\":\\\"%s\\\"", values[2 * i], values[2 * i + 1]))
.collect(joining(";")) + "}";
}
вариант 1
private String generateJsonForOptions(final String... values) {
return "{" + IntStream.range(0, values.length / 2)
.boxed()
.map(i -> String.format("\\\"%s\\\":\\\"%s\\\";", values[2 * i], values[2 * i + 1]))
.reduce("", (s1, s2) -> s1 + s2)
.replaceAll(";$", "}");
}
вариант 2
private String generateJsonForOptions(final String... values) {
String result = IntStream.range(0, values.length / 2)
.boxed()
.map(i -> String.format("\\\"%s\\\":\\\"%s\\\";", values[2 * i], values[2 * i + 1]))
.reduce("", (s1, s2) -> s1 + s2)
.replaceAll(";$", "");
return "{" + result + "}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment