Functional Interface Java8
Reverse String using functional interface in java
public class ReverseStringWithLamda {
@FunctionalInterface
public interface Reverser<T> {
T reverse(T t);
}
public static void main(String[] args) {
Reverser<String> reverser = x -> new StringBuilder(x).reverse().toString();
Stream s = Stream.of("Bikash")
.map(x -> reverser.reverse(x));
System.out.println(s.collect(Collectors.toList()));
/**
*Alternative
*/
Function<String, String> function = x -> new StringBuilder(x).reverse().toString();
Stream s = Stream.of("Bikash")
.map(function)
.collect(Collectors.toList()));
System.out.println(s.collect(Collectors.toList()));
}
}