search
HomeJavajavaTutorialAnalysis of Java lambda expressions and generic application examples

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;
}

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> {
}

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

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:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

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

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

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

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

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

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

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]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

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

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function