JAVAEE_____ Enumerations and Lambda Expressions

Enums and Lambda Expressions

1. Enumeration

1.1 Definitions:

Using enum definitions, enums and classes are at the same level. A value used to represent a group of the same business

1.2 switch judgment

public enum ColorEnom {
    
    
    //枚举命名格式:所有枚举值使用全部字母大写的形式定义
    RED,GREEN,BLACK;
}

insert image description here

insert image description here

public class App {
    
    
    public static void main(String[] args) {
    
    
       printColor(ColorEnom.GREEN);
    }

    public static void printColor(ColorEnom colorEnom){
    
    
        switch (colorEnom){
    
    
            case RED:
                System.out.println("这是红色");
                break;
            case GREEN:
                System.out.println("这是绿色");
                break;
            case BLACK:
                System.out.println("这是黑色");
                break;
            default:
                System.out.println("这是一个未知颜色");
                break;

        }
    }
}

insert image description here

Advantages :
1. High readability
insert image description here

2. Reduced the error rate of parameter passing
insert image description here

3. If enumeration is used, the writing method of switch (multi-conditional judgment) is simpler and the semantics are clearer.
insert image description here

4. The code is more elegant
5. The enumeration has built-in methods, which are more powerful

Disadvantages: not inheritable, cannot be extended

1.3 Common methods of enumeration

1.3.1 using values()

: Returns all members of the enumeration type as an array

public class App {
    
    
    public static void main(String[] args) {
    
    
         eachEnum();

    }
 private static void eachEnum(){
    
    
        ColorEnom[] arrs = ColorEnom.values();
         for(ColorEnom item : arrs){
    
    
          System.out.println(item);
          }
    }
}

1.3.2 ordinal() usage

: Get the index position of the enumeration member

public class App {
    
    
    public static void main(String[] args) {
    
    
     //  printColor(ColorEnom.GREEN);
        eachEnum();

    }
    private static void eachEnum(){
    
    
        ColorEnom[] arrs = ColorEnom.values();
        for(ColorEnom item : arrs){
    
    
            int enumIdx = item.ordinal();

            System.out.println(item + ":" +enumIdx);
        }
    }
}

1.3.3 use of valueOf()

: Convert a normal string to an enum instance

1.3.4 use of compare()

: Compare the order of two enumeration members when they are defined
who compareTo(shui)
subscript – subscript

2. Lambda expressions

2.1 Lambda Definition

Lambda expressions are an important new feature in JDK8. Lambda expressions allow you to replace functional interfaces with expressions (implement business functions through expressions).
A lambda expression is like a method, it provides a normal parameter list and a body that uses those parameters. A
lambda expression can be thought of as an anonymous function

2.2 Why use Lambda expressions

  1. Provides a simpler syntax and way of writing code
  2. replace anonymous inner class
  3. Simplified code, clean and tidy

2.3 Lambda syntax

Basic syntax:
(parameter list) -> (business code implementation)
-> : is used (cannot be omitted)

import java.util.Arrays;
import java.util.List;

public class LambdaTest {
    
    
    public static void main(String[] args) {
    
    
        List<String> List = Arrays.asList("MySQl", "liu");

        //传统
        for (String item : List) {
    
    
            System.out.println(item);
        }
        System.out.println();

        //lambda
        List.forEach((item)->{
    
    
            System.out.println(item);
        }) ;
         //简洁lambda
        List.forEach(item-> System.out.println(item));
    }
}

insert image description here

2.4 Lambda expressions and functional interfaces

2.4.1 Functional interface definition: an interface has one and only one abstract method

2.4.2 Notes:

  1. An interface is a functional interface if it has only one abstract method.
  2. If we declare the @FunctionalInterface annotation on an interface, the compiler will require the interface according to the definition of the functional interface, so if there are two abstract methods, the program compilation will report an error.

2.4.3 Functional Interface Definition Code

@FunctionalInterface
 interface MyFuncationalInterface {
    
    
    void myMethod(Object agrs);


}

2.4.4 The use of Lambda in collections

forEach
List sorted
List lookup in Map

advantage:

  1. Simple code, rapid development
  2. Convenient functional programming
  3. very easy to parallelize
  4. Java introduces Lambda, which improves collection operations.
    Disadvantages:
  5. Poor code readability
  6. In non-parallel computing, many computations may not have higher performance than traditional for
  7. Not easy to debug

Guess you like

Origin blog.csdn.net/biteqq/article/details/123589568