Skip to content

Instantly share code, notes, and snippets.

@dblevins
Created July 26, 2015 23:26
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 dblevins/a2566ffb3f8f04d4b3e1 to your computer and use it in GitHub Desktop.
Save dblevins/a2566ffb3f8f04d4b3e1 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Foo {
public static void main(String... args) {
final List<Book> books = Arrays.asList(
new Book("50 Sheets with Stains"),
new Book("Twitlight")
);
final ArrayList<Movie> movies = new ArrayList<>();
// Convert and consume
books.stream()
.map(Foo::convert)
.forEach(movies::add);
// Show your work
movies.forEach(System.out::println);
}
public static Movie convert(Book book) {
return new Movie(book.getTitle());
}
public static class Book {
private final String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
public static class Movie {
private final String title;
public Movie(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Movie{" +
"title='" + title + '\'' +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment