Hi, everyone. I’m an e-bike racer, an ordinary programming girl.

preface

Recently, when I read Effective Java, I was curious that Lambda is better than anonymous classes in article 42 of the book. I wonder why Lambda is better than anonymous classes. Put forward the following three questions, let’s take a look.

  • What are anonymous classes

  • What is a Lambda

  • What are Lambda’s advantages over anonymous classes

1. What are anonymous classes

1.1 What are Anonymous Classes

Anonymous class, is hidden under the body of others implicitly, inside a class, no name class, do not need to provide any class name can be directly instantiated. Can not be referenced, can only be created with new declaration.

1.2 Implementation of anonymous classes

Anonymous classes can be implemented in two main ways:

  • Anonymous classes inherit from a parent class

     
    class Person {
      public void eat(a) {
          System.out.println("The parent class eat"); }}public  class Children {
    
     public void innerclass(a) {
       // Create an anonymous class that inherits the Person class and overrides the eat method
       Person person = new Person() {
         @Override
         public void eat(a) {
           System.out.println("I'm full and I sleep."); }}; person.eat(); }}Copy the code
  • Anonymous classes implement an interface

    interface Person { public void eat(); } public class Children {public void innerclass() {Person = new Person() {@override public void eat() { System.out.println(" I am full of food and sleep "); }}; person.eat(); }}Copy the code

1.3 When are Anonymous Classes used

  • Before Java 8, when we wanted to use functions as arguments to methods, we had to use anonymous classes. The main way to create function objects is through anonymous classes.

// The Comparator is the interface. It is an abstract policy for sorting
Collections.sort(words, new Comparator<String>() {
   @Override
   public int compare(String o1, String o2) {
     returnInteger.compare(o1.length(),o2.length()); }});Copy the code

As can be seen from the implementation of the anonymous class above, we created one less class. The anonymous object overwrites the Compare method and calls it through the anonymous object to achieve the same effect as the method. This is the function of the anonymous class.

An anonymous inner class has no name, cannot get a type, and can only be used as a superclass or interface type.

2. What are Lambda expressions

Lambda expressions are a new Java8 feature that allows functions to be passed into methods as arguments, simplifying code. It’s very elegant to use.

Collections.sort(words,(o1,o2)->Integer.compare(o1.length(),o2.length()));
Copy the code

3. What are the advantages of Lambda over anonymous classes

  • While both can simplify code, Lambda’s advantages are more obvious

    List<String> strList = new ArrayList<>(); strList.add("aaaa"); strList.add("bbbb"); Collections.sort(strList, new Comparator<String>() {@override public int compare(String o1, String o2) { return Integer.compare(o1.length(),o2.length()); }}); Collections.sort(strList,(s1,s2)-> integer.pare (s1.length(),s2.length())); // Lambda expression collections.sort (strList,(s1,s2)-> integer.pare (s1.length(),s2.length())); Collections.sort(strList, Comparator.comparingInt(String::length)); strList.sort(Comparator.comparingInt(String::length));Copy the code

    From the above, we can see that lambda expressions are solved in one line, which is more concise. victory

  • Lambda is a direct substitute for anonymous classes to some extent, so try to use lambda when writing code later.

  • Lambda is the best way to minimize function objects. This opens the door to functional programming

  • Never use anonymous classes for function objects. Unless you have to create instances of non-functional interfaces.

Note (only anonymous classes, not Lambda)

  • Create an instance of an abstract class, using anonymous classes. Lambda, after all, is limited to function interfaces
  • Create an instance of an interface with multiple abstract methods, using anonymous classes
  • Anonymous class, where this refers to an instance of the anonymous class, the external instance to which Lambda points. If you need to access the internal body of a function object, use anonymous classes.

conclusion

The advent of Java8 ushered in the era of functional programming, and that’s how good it is to use it. Enough nonsense for this post, but the point is to boldly use Lambda instead of anonymous classes. Of course, if you’re still using Java 8 before, please ignore me.

Goodbye, everyone. I’m just a babbling kid.