そのうちの一つがandThen()である.以下の例は引換券のIDを入力として本のタイトルを返す.
package org.tanuneko; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.stream.Collectors.*; public class AndThenSample { private static List<ClaimTicket> tickets; private static void init() { tickets = new ArrayList<ClaimTicket>(); tickets.add( new ClaimTicket( 1, new Book( Janre.Sports, "All about Payton Manning") ) ); tickets.add( new ClaimTicket( 2, new Book( Janre.Sports, "Andy - how to win at playoff") ) ); tickets.add( new ClaimTicket( 3, new Book( Janre.Education, "ABC") ) ); tickets.add( new ClaimTicket( 101, new Book( Janre.Education, "Waldorf education - how it works") ) ); } public static void main( String args[] ) { init(); Function<ClaimTicket,Book> getBookByClaimId = ClaimTicket::getBook; Function<Book,String> getTitle = Book::getTitle; Function<ClaimTicket,String> getTitleOfClaimedBook = getBookByClaimId.andThen(getTitle); System.out.println( tickets.stream().filter(x->x.getId()<100) .map(getTitleOfClaimedBook) .collect( Collectors.reducing((x,y)-> x + "," + y)) .toString() ); } } enum Janre { Education, Sports } class Book { private Janre janre; private String title; public Book( Janre janre, String title ) { this.janre = janre; this.title = title; } public Janre getJanre() { return janre; } public String getTitle() { return title; } } class ClaimTicket { private int id; private Book book; public ClaimTicket( final int id, final Book book ) { this.id = id; this.book = book; } public int getId() { return id; } public Book getBook() { return book; } }