Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philipjkim/2cd3abe7550fa7bdadb7ed139d863baf to your computer and use it in GitHub Desktop.
Save philipjkim/2cd3abe7550fa7bdadb7ed139d863baf to your computer and use it in GitHub Desktop.
Java - Transformation from 2 arrays to a map - Imperative vs. Functional
import static org.junit.jupiter.api.Assertions.*;
class FunctionalVsIemerativeTransformationTest {
@Test
void functionalVersusImperative() {
// Imperative
var parameterNames = new String[]{"aaa", "bbb"};
var args = new Object[]{"arg1", "arg2"};
Map<String, Object> params1 = new HashMap<>();
for (int i = 0; i < parameterNames.length; i++) {
params1.put(parameterNames[i], args[i]);
}
// Functional
Map<String, Object> params2 = IntStream.range(0, parameterNames.length)
.mapToObj(x -> new Pair<>(parameterNames[x], args[x]))
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
assertEquals(params1, params2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment