Created
December 2, 2022 01:46
-
-
Save philipjkim/2cd3abe7550fa7bdadb7ed139d863baf to your computer and use it in GitHub Desktop.
Java - Transformation from 2 arrays to a map - Imperative vs. Functional
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
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