Skip to content

Instantly share code, notes, and snippets.

@m-doi
Created July 13, 2017 00:42
Show Gist options
  • Save m-doi/b872e281dfbdf042762fb29a54445ca0 to your computer and use it in GitHub Desktop.
Save m-doi/b872e281dfbdf042762fb29a54445ca0 to your computer and use it in GitHub Desktop.
package com.doilux.generics;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Odai {
public static void printA(List<Int> l) {
l.forEach(Int::print);
}
public static <T extends Printable> void printB(List<T> l) {
l.forEach(Printable::print);
}
public static <T extends HasTwice<T>> List<T> twiceB(List<T> l) {
return l.stream()
.map(HasTwice::twice)
.collect(Collectors.toList());
}
public static <T extends HasTwice<T> & Printable<T>> List<T> twiceAndPrint(List<T> l) {
return l.stream()
.map(HasTwice::twice)
.peek(v -> v.print())
.collect(Collectors.toList());
}
public static <T extends HasTwice<T> & Printable<T>, R extends HasTwice<T> & Printable<T>> List<R>
applyAndPrint(List<T> l, Function<HasTwice<?>, R> g) {
return l.stream()
.map(g)
.peek(v -> v.print())
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment