java container generics

catalogue

1: Introduction to generics

  1.1: basic concepts of generics

1.2: benefits of generics

1.3 type erasure

2: Use of generics

2.1 defining generics

2.2: generic classes

code

2.3: Generic interface

code

2.4: generic methods

  code

Static method

  code

2.5: generic methods and variable parameters

code

1: Introduction to generics

  1.1: basic concepts of generics

The essence of generics: placeholders for data types. Generics parameterize data types and can be passed in like parameters

Abbreviation:

● pass the type as a parameter

● < data type > can only be reference type

1.2: benefits of generics

When using generics, all type conversions are done by the compiler

● better code readability (no forced type conversion)

● the program is safer (as long as the compiler has no warnings, there will be no exceptions at runtime)

1.3 type erasure

● type parameters written in generics during encoding will be removed by the compiler during compilation, which is called "type erasure"

● generics are mainly used in the compilation stage. The bytecode class file generated after compilation does not contain the type information in generics

● the type conversion involved is still an ordinary forced type conversion. The type parameter will be replaced with Object after compilation. The virtual machine does not know the generic type at runtime

2: Use of generics

2.1 defining generics

Generic characters can be any identifier, but generally E, T, K, V, N? To mark

●E   Element is used in the container to represent the elements in the container

●T   Typr represents a normal java class

● K Key indicates build, for example: build Key in Map

● V value indicates the value

● N Number indicates the value type

●? Represents an uncertain numeric type

2.2: generic classes

● generic class is to define the generic type on the class, and the user can determine the type of the generic type only by using the class

● the specific method is to add one or more type parameter declarations after the class name, such as < E >, < e, t, K, V, n >

code

package com.gnlxy.container;

public class Generics<T> {
    private  T flag;

    public void setFlag(T flag) {
        this.flag = flag;
    }

    public T getFlag() {
        return flag;
    }

}
public class Test {
    public static void main(String[] args) {
        Generics<String>G=new Generics<>();
        G.setFlag("Wang Tingbi reward");
        String name=G.getFlag();
        System.out.println(name);
        Generics<Integer>I=new Generics<>();
        I.setFlag(1000000);
        Integer number=I.getFlag();
        System.out.println(number);


    }
}

2.3: Generic interface

The form of generic interface is the same as that of generic class, but the specific type of generic interface should be declared in the implementation class

code

 

interface Igenerics<T> {
    T Getname(T name);


}
package com.gnlxy.container;

public class IgenericsImpl implements Igenerics<String> {
    @Override
    public String Getname(String name) {
        return name;
    }
}
public class Test2 {
    public static void main(String[] args) {
        IgenericsImpl i=new IgenericsImpl();
       String name= i.Getname("Shu Chang Chen");
        System.out.println(name);
        Igenerics<String>ii=new IgenericsImpl();
        String num=ii.Getname("1000000");
        System.out.println(num);

    }
}

2.4: generic methods

Generic method refers to defining the parameter type of a method as generic so that it can be used to receive different types of parameters when called

  code

package com.gnlxy.container;

public class MethodGenerics {
    public<T> void Setname (T name){
        System.out.println(name);

    }
    public<T> T Getname(T name){
        return name;
    }
}
package com.gnlxy.container;

public class Test3 {
    public static void main(String[] args) {
        MethodGenerics M=new MethodGenerics();
        M.Setname("Lu Xinyu wants to take the postgraduate entrance examination!!!!");
        MethodGenerics Mm=new MethodGenerics();
        Integer I=Mm.Getname(6);
        System.out.println(I);
    }
}

Static method

Static methods cannot access generics defined on a class

  code

  //Static method generics
    public static <T> void SetFlag(T Flag){
        System.out.println(Flag);
    }
    public static <T> T GetFlag(T Flag){
        return Flag;
    }
}
public class Test4 {
    public static void main(String[] args) {
        MethodGenerics.SetFlag("Lu Xinyu will be admitted to 211!!!!!");
       Integer i= MethodGenerics.GetFlag(6);
        System.out.println(i);
    }
}

2.5: generic methods and variable parameters

code

 
    public <T> void method(T...args){//Variable parameter type... Parameter name
        for(T t:args){//Enhanced for loop
            System.out.println(t);
        }
public class Test5 {
    public static void main(String[] args) {
        MethodGenerics methodGenerics=new MethodGenerics();
        String[] arr=new String[]{"a","b","c"};
        methodGenerics.method(arr);
        Integer[] arr2=new Integer[]{1,2,3};
        methodGenerics.method(arr2);
    }
}

 

 

Tags: Java

Posted on Tue, 21 Sep 2021 15:28:03 -0400 by CookieD89