Previous Lecture Lecture 16 Next Lecture

Lecture 16, Mon 06/03

Lambda Expressions

Lambda Expressions

public class Lecture {
    public static void makeSound(String animal) {
        switch(animal) {
        case "dog":
            System.out.println("BARK!");
            break;
        case "cat":
            System.out.println("MEOW!");
            break;
        // ... and so on ...
        default:
            System.out.println("??");
        }
        return;
    }

    public static void main(String[] args) {
        makeSound("dog");
    }
}
// Animal.java
public interface Animal {
    public void sound();
}
// Dog.java
public class Dog implements Animal {
    @Override
    public void sound() {
        System.out.println("BARK!");
    }
}
// Cat.java
public class Cat implements Animal {
    @Override
    public void sound() {
        System.out.println("MEOW!");
    }
}
// Lecture.java
public class Lecture {
    public static void makeSound(Animal animal) {
        animal.sound();
    }

    public static void main(String[] args) {
        makeSound(new Cat()); // Example of an anonymous Object
        makeSound(new Dog());
    }
}

Anonymous Classes

Example

// Animal.java

// @FunctionalInterface is optional, but is used for
// protection. Will not compile if the interface has more
// than one abstract method.

@FunctionalInterface
public interface Animal {
    public void sound();
}
import java.util.ArrayList;

public class Lecture {
    public static void makeSound(Animal animal) {
        animal.sound();
    }

    public static void main(String[] args) {
        //Animal a = new Animal(); // ERROR!
        
        // Anonymous class representing a dog
        Animal dog = new Animal() {
            public void sound() {
                System.out.println("BARK!");
            }
        };
        // Anonymous class representing a cat
        Animal cat = new Animal() {
            public void sound() {
                System.out.println("MEOW!");
            }
        };
        // Anonymous class representing a cow
        Animal cow = new Animal() {
            public void sound() {
                System.out.println("MOO!");
            }
        };
        
        ArrayList<Animal> animalList = new ArrayList<Animal>();
        animalList.add(dog);
        animalList.add(cat);
        animalList.add(cow);
        
        for (Animal a : animalList) {
            makeSound(a);
        }
    }
}
animalList.add(new Animal() {
    public void sound() {
        System.out.println("CAW!");
    }
});

Lambda Expressions

Example of converting sound() to a lambda expression

public void sound();

Animal dog = () -> { System.out.println("BARK!"); };
Animal cat = () -> { System.out.println("MEOW!"); };
Animal cow = () -> { System.out.println("MOO!"); };
Animal dog = () -> System.out.println("BARK!");
Animal cat = () -> System.out.println("MEOW!");
Animal cow = () -> System.out.println("MOO!");

Example of Lambda Expressions with parameter

// @FunctionalInterface is optional, but is used for
// protection. Will not compile if the interface has more
// than one abstract method.

@FunctionalInterface
public interface Animal {
    public void sound(double weight);
}
// Lecture.java
public class Lecture {
    public static void makeSound(Animal animal) {
        animal.sound(60);
    }

    public static void main(String[] args) {
        Animal dog = (double weight) -> {
            if (weight > 50)
                System.out.println("BARK!!!!!");
            else
                System.out.println("BARK!");
        };
        Animal cat = (double weight) -> {
            if (weight > 50)
                System.out.println("MEOW!!!!!");
            else
                System.out.println("MEOW!");
        };	
        Animal cow = (double weight) -> {
            if (weight > 50)
                System.out.println("MOO!!!!!");
            else
                System.out.println("MOO!");
        };

        ArrayList<Animal> animalList = new ArrayList<Animal>();
        animalList.add(dog);
        animalList.add(cat);
        animalList.add(cow);
        
        for (Animal a : animalList) {
            makeSound(a);
        }
    }
}
Animal dog = (weight) -> {
    if (weight > 50) System.out.println("BARK!!!!!");
    else System.out.println("BARK!");
};

Animal cat = (weight) -> {
    if (weight > 50) System.out.println("MEOW!!!!!");
    else System.out.println("MEOW!");
};	

Animal cow = (weight) -> {
    if (weight > 50) System.out.println("MOO!!!!!");
    else System.out.println("MOO!");
};

Implementing Collections.Sort with Lambda Expressions

// Student.java
public class Student {
    private String name;
    private int perm;

    public Student(String name, int perm) {
        this.name = name;
        this.perm = perm;
    }

    public String getName() { return name; }
    public int getPerm() { return perm; }
}
// Lecture.java
    public static void main(String[] args) {
        ArrayList<Student> a = new ArrayList<Student>();
        a.add(new Student("RICHERT", 80498567));
        a.add(new Student("MIKE", 1234567));
        a.add(new Student("TANYA", 3333333));
        a.add(new Student("ZORRA", 5555555));

        // Anonymous class
        Collections.sort(a, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                Student x = (Student) o1;
                Student y = (Student) o2;
                if (x.getPerm() > y.getPerm())
                    return 1;
                else if (x.getPerm() == y.getPerm())
                    return 0;
                else 
                    return -1;
            }
        });
        
        for (Student s : a) {
            System.out.println(s.getName() + ": " + s.getPerm());
        }
    }
Collections.sort(a, (s1, s2) -> {
    if (s1.getPerm() > s2.getPerm())
        return 1;
    else if (s1.getPerm() == s2.getPerm())
        return 0;
    else 
        return -1;
});

Example using lambda expressions implementing the Runnable Interface

public class Lecture {
    public static void main(String[] args) {
        
        Thread t1 = new Thread(() -> {
            int i = 0;
            while (true) {
                ++i;
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        
        t1.start();
    }
}