Lambda Expression

First snippets

List names = Arrays.asList("Nguyen Thi A", "Tran Thi B", "Le Thi C");

for (String name : names) {
    if (name.contains("Tran"))
        System.out.println(name);
}

And

List names = Arrays.asList("Nguyen Thi A", "Tran Thi B", "Le Thi C");

Predicate predicate = s -> s.contains("Tran");
names.stream().filter(predicate).forEach(System.out::println);

It’s an example filter use Predicate

Create data use Supplier

Supplier supplier = () -> (int) (Math.random() * 1000);
for (int i = 0; i < 10; i++)
    System.out.println(supplier.get());

Performs operation on the give argument with Consumer

Consumer c = (x) -> System.out.println(x + 100);
Consumer c2 = (x) -> System.out.println(x + 1);
c.accept(123);
c.andThen(c2).accept(100);

Represents a function that accepts one argument and produces a result.

Function<String, Integer> toInt = Integer::parseInt;
int val = toInt.apply("4");
System.out.println(val);

 

Advertisements