Analysis of Java lambda expressions and generic application examples
Generic
Generic syntax definition
class class name
public class GenericDemo<K> { //定义数组 private K[] arr; }
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()
- 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
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 typeGeneric 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
- 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
- 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
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function