Java basics (generic understanding)

1. Generic overview

The essence of generics is the parameterized type, which means that the data type being operated on is specified as a parameter type.

Second, the generic class

Define a generic class and add a pair of <> after "class name", which defines "generic name";

Format: such as ArrayList collection

Examples:

class ArrayList<E> {
	public boolean add(E e) { }
	public E get(int index) { }
}

Use generic classes: When creating an object, determine the type of the generic

For example: ArrayList <String> list = new ArrayList <String> (); At this time, it is equivalent to replace E type with String type.

Third, use generics in the collection

Use generics to specify that the collection element type must be a certain type

Examples:

        List<String> list=new ArrayList<>();
        list.add("yyy");
        list.add("wtc");
        for(int i=0;i<list.size();i++){
            String s=(String)list.get(i);
            System.out.println("获取的值为:"+s);
        }
        //aslist:将多个数据转成集合
        List<Integer> list1 = Arrays.asList(1, 2, 3);
        List<String> list2 = Arrays.asList("y", "w", "a");

Fourth, the generic method

Definition format: modifier <represents generic method> Return value type method name ( generic identifier parameter name) {}

Examples:

mport java.util.ArrayList;
import java.util.List;

public class demo_泛型方法 {
    public static void main(String[] args) {
        //任何同类型的数据类型都可以,不在局限于只能传一种类型的数据
        List list=ArrayTools.toList("w","y");
        System.out.println(list);
        List list1=ArrayTools.toList(1,2,3);
        System.out.println(list1);
    }
}
class ArrayTools{
           //...表示参数个数任意即可变参数
    public static <T> List toList(T...e){
        List<T> list=new ArrayList<>();
        for(int i=0;i<e.length;i++){
            list.add(e[i]);
        }
        return list;
    }
}

operation result:

Five, generic interface

Format: permission modifier interface interface name <generic identifier> {}

Examples:

public class demo_泛型接口 {
    public static void main(String[] args) {
        Inter<String> inter=new InterImpl();
    }
}
interface Inter<T>{
    T getData(T t);
}
class InterImpl<T> implements Inter<T>{
    @Override
    public T getData(T s) {
        return null;
    }
}

6. Generic wildcards

Type wildcards generally use? Instead of specific type parameters.

For example, List <?> Is logically List <String>, List <Integer> and all other List <concrete type arguments> classes.

Examples:

import java.util.ArrayList;
import java.util.List;

public class demo_泛型通配符 {
    public static void main(String[] args) {
       List<String> strings=new ArrayList<>();
       m1(strings);
       List<Person> people=new ArrayList<>();
       List<Student> students=new ArrayList<>();
       List<Teacher> teachers=new ArrayList<>();
       m2(people);
       m2(students);
       m3(people);
       //m3(teachers);报错
    }
    //任何类型的参数都可以
    public static void m1(List<?> list){}
    //list的类型只能是Person以及Person的子类
    public static void m2(List<?extends Person> list){}
    //只能是Student类型或者是Student的父类类型
    public static void m3(List<?super Student> list){}
}
class Person{}
class Student extends Person{}
class Teacher extends Person{}

Seven, the benefits of using generics

Allows you to eliminate mandatory type conversions in your code, and at the same time get an additional type checking layer that prevents someone from saving keys or values ​​of the wrong type in the collection. This is what generics do.

  1. Type safety
  2. Eliminate mandatory type conversion
Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/96476646