Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Writing in the front

  • When you think of new features in Java8, the first thing you think of is Lambda expressions and functional interfaces. Did he “optimize” the simplicity of the code in any way? Or maybe ta makes reading and debugging more difficult for programmers to some extent, causing headaches for many programmers. Next up on “Talking About Java,” the new feature is Lambda, which we both love and hate.

Lambda expressions

Essentially a functional programming concept that returns an implementation of an interface

Applications in threads

The traditional way

Create a disposable class

// A disposable class that acts as an implementation class for Runnable pairs in new threads
class runnable implements Runnable{

    @Override
    public void run(a) {
        System.out.println("I'm on my way."); }}public class lambdaTest {
    public static void main(String[] args) {
        runnable runnable = new runnable();
        Thread thread1 = newThread(runnable); }}Copy the code

(slightly optimized) Anonymous inner classes

Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run(a) {
                System.out.println("I'm on my way."); }});Copy the code

role

  • Avoid overdefining anonymous inner classes
  • Make the code look simple
  • Simplify the code, leaving only the core logic

Functional interface

Definition: Any interface that contains only one abstract method is a functional interface

public interface Runnable{
    void run(a);
}
Copy the code

** @functionalInterface ** can be declared at the top of the interface class

All Lambda types are interfaces

  • The Lambda expression itself is an implementation of this interface

If defined as an implementation class, an error is reported

  • When the program is compiled, we don’t know which subclass of the object we finally create until the runtime. Then we can make the like point to a variety of different classes and call a variety of different subclass methods, which greatly improves the scalability of the program

Whereas lambda here is actually equivalent to an anonymous inner class (without a class name), we don’t know what class is actually created, so we’ll define it as an interface that takes advantage of the uptransition properties of polymorphism. More about polymorphism in my other blog post: Portal ->

Method references

Demo

 // Interface definition
    interface parseIntNum{
        // Define a method to convert a String to an Integer
        int pass(String s);
    }
    public static void main(String[] args) {
        parseIntNum parseIntNum1;
        parseIntNum parseIntNum2;
        / / the original lambda
        parseIntNum1 = (str)-> Integer.parseInt(str);
        System.out.println(parseIntNum1.pass("1"));
        // Method references improved versions
        parseIntNum2 = Integer::parseInt;
        System.out.println(parseIntNum2.pass("1"));
    }
Copy the code

By method reference, you mean if someMethods that already exist,hisThe signatureandThe functions defined in the interfaceIf it does, the method reference can be passed in directly. Because the methods defined by the parseIntNum interface are int pass(String s) andIntegerCompared to the static method int parseInt(String s) inThe method parametersConsistent,The return typeSame. That’s the way we’re talking about itThe signature is consistent, you can pass method references directly

Method reference

  • It’s actually quite simple, just use the operator double colon ** “::”**

Common method references

Common reference forms

Class name :: Static method name

Call a static method of a class

  • Using Integer::parseInt is equivalent to calling Integer’s static method parseInt

Object: Instance method

That’s a little bit of an HHHH

class Naruto{
    public static void Rasengan(a){
        System.out.println("Spiral pill"); }}// Interface definition
interface Ninja{
    // Define an arcane method
    void aoyi(a);
}
public class methodQuote {
    // By referencing Naurto's spiral pills
    Ninja ninja=Naruto::Rasengan;
    // Start the auyi again
    ninjia.aoyi();
}
Copy the code

Data type :new

 public static void main(String[] args) {
     / / way
        IntFunction<int []> arr1 = new IntFunction<int[] > () {@Override
            public int[] apply(int num) {
                return new int[num]; }}; arr1.apply(10);
     // Method 2 (method reference)
        IntFunction<int []> arr2 = int[] : :new;
        arr2.apply(10);

    }
Copy the code
  • There are many other forms, but I won’t go into them here

The development of the commonly used

Commonly used small skills: traversal number group printing

A comparison of the three methods shows the degree of simplicity

ArrayList<Integer> arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
// Anonymous inner class
        arrayList.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) { System.out.println(integer); }});/ / lambda optimization
		arrayList.forEach((integer)-> System.out.println(integer));	
// Method reference prints, replacing our anonymous inner class with method references (equivalent to replacing lambda)
        arrayList.forEach(System.out::println);
Copy the code

Question to solve

Intger.parseint = intger.parseint = intger.parseint = intger.parseint = intger.parseint = intger.parseint = intger.parseint

  • In fact, I confused lambda,lambda is defined when the parameter, is not the actual parameter

So that parameter, you could say, is just serving the body of the method, it’s just being used in the body of the method. We use method references, provided that the parameters are the same as the return value, and the method body is what we want to implement, so naturally we don’t need to write the method body, and the parameters that the method body depends on don’t need to be used

Write in the last

  • I will start working on the project recently. I may have a deeper grasp of these basic knowledge when PREPARING for the interview in the future. I don’t have a deep understanding at present, so please point out any mistakes.

Write in the last

  • This column will update some Java basic knowledge and interviews intermittently, to lay a foundation for future interviews in advance!