Created
August 13, 2019 22:52
-
-
Save snekse/029f17a42b79d3aba6f25f23604c49d4 to your computer and use it in GitHub Desktop.
Java streaming
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
package com.snekse.java.utils | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public class CollectionStreamUtil { | |
public static <T, R> List<R> mapToList(Collection<T> i, Function<T, R> f) { | |
return i.stream().map(f).collect(Collectors.toList()); | |
} | |
} |
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
package com.snekse.java.utils | |
import org.junit.Test; | |
import java.util.Arrays; | |
import java.util.List; | |
public class CollectionStreamUtilTest { | |
@Test | |
public void mapToList() { | |
List<Integer> result = CollectionStreamUtil.mapToList(Arrays.asList("One", "Two", "Three", "Four"), String::length); | |
int sum = result.stream().mapToInt(Integer::intValue).sum(); | |
assert 154 == sum; | |
// assertEquals(15, sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment