Skip to content

Instantly share code, notes, and snippets.

@nkalra0123
Created January 19, 2020 06:27
Show Gist options
  • Save nkalra0123/1715dd0fa18af8159d954874ba72cfa9 to your computer and use it in GitHub Desktop.
Save nkalra0123/1715dd0fa18af8159d954874ba72cfa9 to your computer and use it in GitHub Desktop.
Functional Interfaces, Lamda and Method References
import java.util.Optional;
import java.util.function.*;
import java.util.stream.Stream;
public class FunctionalInterfaces {
@FunctionalInterface
public interface foo
{
abstract public void food();
@Override
abstract public String toString();
}
public static void main(String[] args) {
String[] strings = new String[]{"a","b","1","2","3","d"};
// UnaryOperator
Stream<Integer> iterate = Stream.iterate(1, new UnaryOperator<Integer>() {
@Override
public Integer apply(Integer integer) {
return integer + 1;
}
});
System.out.println("iterate.findFirst().get() = " + iterate.findFirst().get());
Stream<Integer> iterate2 = Stream.iterate(1, integer -> integer + 1);
System.out.println("iterate2.findFirst().get() = " + iterate2.findFirst().get());
// BinaryOperator
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
}).get());
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce((x, y) -> x + y).get());
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce(Integer::sum).get());
// Predicate
//Evaluates this predicate on the given argument
System.out.println("Predicate = " + Stream.of(1,2,3,4).filter(new Predicate<Integer>() {
@Override
public boolean test(Integer x) {
return x % 2 == 0;
}
}).findFirst().get());
System.out.println("Predicate = " + Stream.of(1,2,3,4).filter(x -> {
return x % 2 == 0;
}).findFirst().get());
System.out.println("Predicate = " + Stream.of(1,2,3,4).filter(x -> x % 2 == 0).findFirst().get());
// Function, Consumer
Stream.of(strings).filter(s -> s.matches("\\d+")).map(new Function<String, Integer>() {
/**
* Applies this function to the given argument.
*
* @param s the function argument
* @return the function result
*/
@Override
public Integer apply(String s) {
return Integer.parseInt(s);
}
}).forEach(new Consumer<Integer>() {
/**
* Performs this operation on the given argument.
*
* @param o the input argument
*/
@Override
public void accept(Integer o) {
System.out.println(o);
}
});
Stream.of(strings).filter(s -> s.matches("\\d+")).map(s -> Integer.parseInt(s)).forEach(s-> System.out.println(s));
Stream.of(strings).filter(s -> s.matches("\\d+")).map(Integer::parseInt).forEach(System.out::println);
//Supplier
Optional<Integer> value = Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
});
value.orElseGet(new Supplier<Integer>() {
@Override
public Integer get() {
return -1;
}
});
Optional<Integer> value2 = Stream.of(1,2,3,4).reduce((x, y) -> x + y);
value2.orElseGet(() -> -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment