Skip to content

Instantly share code, notes, and snippets.

@trecloux
Created December 20, 2012 21:37
Show Gist options
  • Save trecloux/4348782 to your computer and use it in GitHub Desktop.
Save trecloux/4348782 to your computer and use it in GitHub Desktop.
Java 8 lambda type inference question
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static org.fest.assertions.Assertions.assertThat;
public class LambdaSample {
private final List<String> names = asList("John", "Shaun", "Jeremy", "Richard", "Thomas");
@Test
public void shouldFilter() throws Exception {
List<String> filtered = names.stream()
.filter(x -> x.startsWith("J"))
.into(new ArrayList<>()); // -> Does not compile, needs explicit typing : .into(new ArrayList<String>());
assertThat(filtered).containsOnly("John", "Jeremy");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment