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

Analysis of Java lambda expressions and generic application examples

PHPz
Release: 2023-05-03 18:16:07
forward
1063 people have browsed it

Generic

Generic syntax definition

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

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

Generic usage syntax

  • ##Class name Object Name = new class name ()

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

  • Class name Object name = new Class name<>()

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> {
}
Copy after login

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 {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("多线程就绪");
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template