Skip to the content.

Lambda Expressions

-

What is a Lambda Expression for?

A simple example sans lambda

public interface FileFilter {
    boolean accept(File file);
}

- Implement the interface

public class JavaFileFilter implements FileFilter{
    public boolean accept(File file){
        return file.getName().endsWith(".java");
    }
}

- Usage example

JavaFileFilter fileFilter = new JavaFileFilter();
File dir = new File("d:/tmp");
File[] javaFiles = dir.listFiles(fileFilter);

-

What is a Lambda Expression for?

  1. To make instances of anonymous classes easier to write, and thereby easier to read
FileFilter filter = (File file) -> file.getName().endsWith(".java");

-

Several ways to implement a lambda Expression

FileFilter filter = (File file) -> file.getName().endsWith(".java");
Runnable r = () -> {
	for (int i = 0; i < 5; i++){
		System.out.println("Hello world!");
	}
};
Comparator<String> c =
	(String s1, String s2) ->
		Integer.compare(s1.length(), s2.length());

-

Three Questions About lambdas

-

What is the type of a lambda expression?

-

Functional Interface

A function interface is an interface with only one abstract method

public interface Runnable {
	run();
};
public interface Comparator<T> {
	int compare(T t1, T t2);
};
public interface FileFilter {
	boolean accept(File pathname);
};

- A function interface can be annotated

@FunctionalInterface
public interface MyFunctionalInterface {
	someMethod();
}

This is just a convenience, the compiler can tell me whether the interface is functionl or not

-

Three Questions About lambdas

What is the type of a lambda expression?

Answer: a function interface

-

Three Questions About lambdas

-

Can a lambda be put in a variable

-

Three Questions About lambdas

-

Is a lambda expression an object

-

Three Questions About lambdas

-

Functional Interface Package

-

Supplier

@FunctionalInterface
public interface Supplier<T> {
	T get();
}

A single interface that doesn’t take any object and that provides a new object.

-

Consumer

@FunctionalInterface
public interface Consumer<T>{
	void accept(T t);
}

The consumer accepts an object and doesn’t return anything. (System.out.println)

BiConsumer

@FunctionalInterface
public interface BiConsumer<T, U> {
	void accept(T t, U u);
}

takes two objects instead of one

-

Predicate

@FunctionInterface
public interface Predicate<T> {
	boolean test(T t);
}

A predicate takes an object as a parameter and returns a boolean

BiPredicate

@FunctionalInterface
public interface BiPredicate<T, U> {
	boolean test(T t, U u);
}

-

Function

@FunctionaInterface
public interface Function<T, R> {
	R apply (T t);
}

A function takes an object as a parameter and returns another object

BiFunction

@FunctionalInterface
public interface BiFunction<T, U, R> {
	R apply (T t, U u);
}

-

More Lambda Expressions Syntax

Most of the time, parameter types can be omitted

Comparator<String> c = 
	(String s1, String s2) ->
		Integer.compare(s1.lingth(), s2.length());

Becomes:

Comparator<String> c =
	(s1, s2) ->
		Integer.compare(s1.length(), s2.length());

-

Method References

Consumer<String> c = s -> System.out.println(s);

Consumer<String> c = System.out::println;

It is composed of the object System.out and the method println seperated by ::. This is just a shortcut to writting a lambda expression

-

Comparator<Integer> c = (i1, i2) -> Integer.compare(i1, i2);

Comparator<Integer> c = Integer::compare;