Created
July 26, 2015 23:26
-
-
Save dblevins/a2566ffb3f8f04d4b3e1 to your computer and use it in GitHub Desktop.
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 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