Skip to content

Instantly share code, notes, and snippets.

@snekse
Created August 13, 2019 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snekse/029f17a42b79d3aba6f25f23604c49d4 to your computer and use it in GitHub Desktop.
Save snekse/029f17a42b79d3aba6f25f23604c49d4 to your computer and use it in GitHub Desktop.
Java streaming
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());
}
}
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