Home  >  Article  >  Java  >  Analysis of Java lambda expressions and generic application examples

Analysis of Java lambda expressions and generic application examples

PHPz
PHPzforward
2023-05-03 18:16:07980browse

Generic

Generic syntax definition

class class name78ea81a9b03028c4246789300a0203aa{ private generic identifier 1, variable name; commonly used Generic identifier: T, E, K, V } Normal writing inside

public class GenericDemo<K> {
    //定义数组
    private K[] arr;
}

Generic usage syntax

  • ##Class name c072433e2612cda9cec45bbc4b2c1413 Object Name = new class name 2c5c1913d8605a5bcae8186b3c13288c ()

  • After jdk1.7, the following a8093152e673feb7aba1828c43532094 can be omitted without writing

  • Class namec072433e2612cda9cec45bbc4b2c1413 Object name = new Class namea8093152e673feb7aba1828c43532094()

Generic summary

  • Type parameterization makes class definitions more universal

  • Generics do not support basic types because basic types cannot be converted to Object at compile time

  • When a generic class creates an object, if no data type is specified, it will operate according to the Object type

  • The same generic class can be logically viewed as multiple Different types, but actually the same type

Derive subclasses from generic classes

Subclasses are also generic, and the generics of subclasses and parent classes must be Consistent (for example: even the generic type T must be consistent, and the written content must be the same),

If the parent class does not specify a type, the parent class must handle it according to the Object type

public class Child<T> extends Parent<T> {
}

The subclass is not Generics, the parent class must specify the generic type,

If the parent class does not specify the type, the parent class must handle it according to the Object type

Generic interface

  • The syntax definition of generic interface: interface interface name 1ddbd52973931b5164a458f811537cca{generic identification method name (); … }

  • The implementation class is also generic. The generics of the implementation class and the interface must be consistent. If the interface does not specify a type, the interface must be processed according to the Object type.

  • The implementation class is not generic. The interface must clarify the data type of the generic interface. If the interface does not specify a type, the interface will be processed according to the Object type

lambda expression

lambda expression concept

  • Object-oriented programming emphasizes that things must be done in the form of objects; function thinking tries to ignore the complex writing of objects and "emphasizes what to do, not the form in which to do it"

  • lambda expression is the embodiment of function idea

  • lambda expression is a simplified way of defining a functional expression, not a simplified way of writing a function call

lambda expression format

  • Three elements: formal parameters, arrows, code blocks

  • Form For example: (formal parameters) ->Code block

  • Formal parameters If there are multiple parameters, separate them with commas. If there are no parameters, leave empty brackets and no spaces.

  • -> Must be in English, fixed writing method, indicating execution of

  • code block: specific things to be done

  • Prerequisite for use: Use interface: There is and is only one abstract method in the interface

Code example:

package com.lambda;
public class Lambda {
    public static void main(String[] args) {
        //正常写法
        Mythread mythread = new Mythread();
        Thread t = new Thread(mythread);
        t.start();
        //匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("多线程1就绪了");
            }
        }).start();
        //lambda表达式:创建线程
        new Thread(()->{
            System.out.println("多线程2就绪了");
        }).start();
    }
}
class Mythread implements Runnable{
    @Override
    public void run() {
        System.out.println("多线程就绪");
    }
}

The above is the detailed content of Analysis of Java lambda expressions and generic application examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete