I’m revising for the OCP (Oracle Certified Professional Java Programmer ), and running over Lambdas at the moment. In tutorials I see many of them seem to focus on what they are , what they do , streams etc.
Not much on how they can be used to improve code ( and making less Class files in particular ).
Here we have an Animal class that has a couple of subclasses. The Animal subclasses will print out their behaviour polymorphically (and its all abit of a mess). I won’t get into Inheritance, classes and composition. But I will say less classes = better ( sometimes hard to avoid in Java ).
The Animal subclasses will print out their behaviour polymorphically.
Complex way
public class Animal { private String name; public Animal(String name) { this.name = name; } public void printMyBehaviour() { /* subclasses implement */ } public static void main(String[] args) { Animal shark = new Fish("Shark"); Animal roo = new Kangaroo("Kangaroo"); Animal man = new Man("Human"); shark.printMyBehaviour(); roo.printMyBehaviour(); man.printMyBehaviour(); } } public class Fish extends Animal { public Fish(String name) { super(name); } public void printMyBehaviour() { System.out.print("I can Swim"); } } public class Kangaroo extends Animal { public Kangaroo(String name) { super(name); } public void printMyBehaviour() { System.out.print("I can Jump"); } } public class Man extends Animal { public Man(String name) { super(name); } public void printMyBehaviour() { System.out.print("I can run"); } }
So here you have 4 class files (Animal, Fish, Kangaroo, Man), you could easily put printMyBehaviour in an inferface also that would make 5
Simple Way Lambdas
Here is how Lamdbas can improve this ( note I’ve used the oven ready Consumer Lambda included java.util.function ). In preference to making my own Functional interface (less classes if possible remember).
public class Animal { String name; public Animal(String name) { super(); this.name = name; } public static void main(String[] args) { Animal shark = new Animal("Shark"); Animal roo = new Animal("Kangaroo"); Animal man = new Animal("Human"); print(shark, s->System.out.println(s.name+" I swim ")); print(roo, s->System.out.println(s.name+" I jump ")); print(man, s->System.out.println(s.name+" I run ")); } public static void print(Animal ani, Consumer<Animal> consumer){ consumer.accept(ani); } }
Lambdas are great for a couple of lines of code (but if its more than 2 or 3 lines of code, I’d stick to making them in their own method the old way, rather than using lambdas, them inline like this).
They can get counter productive the (hard to read, there’s a tendency to cram stuff in and make it less readable, something I’ve seen way to often in Javascript this is especially true for arrow functions, anonymous functions…
Handy Lambda classes / Function interfaces
There are a number of existing Lambdas for common things ( not just in the streams API ), for example the ones below (use an existing one if it makes sense, less new Class/Interface files ! ):
- Predicate
- Consumer
- Supplier
- Comparator