Home > Java > javaTutorial > Introduction to Java Generics

Introduction to Java Generics

伊谢尔伦
Release: 2017-01-24 14:57:50
Original
1428 people have browsed it

1. The introduction of the concept of generics (why are generics needed)?

First, let’s look at the following short code:

public class GenericTest {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("qqyumidi");
        list.add("corn");
        list.add(100);
        for (int i = 0; i < list.size(); i++) {
            String name = (String) list.get(i); // 1
            System.out.println("name:" + name);
        }
    }
}
Copy after login

defines a collection of List type, first adds two string type values ​​to it, and then Add a value of type Integer. This is completely allowed, because the default type of list is Object. In subsequent loops, errors similar to //1 may easily occur due to forgetting to add Integer type values ​​to the list before or for other encoding reasons. Because the compilation phase is normal, but a "java.lang.ClassCastException" exception occurs during runtime. Therefore, such errors are difficult to detect during coding.

Before Java SE 1.5, in the absence of generics, the "arbitrary" parameters were implemented by referencing the type Object. The disadvantage of "arbitrary" was that explicit coercion was required. Type conversion, and this conversion requires the developer to know the actual parameter type beforehand. For forced type conversion errors, the compiler may not prompt an error, and an exception will occur during runtime. This is a security risk. The advantage of generics is that type safety is checked during compilation, and all casts are automatic and implicit, improving code reuse.

2. What are generics?

Generics are a new feature of Java SE 1.5. The essence of generics is a parameterized type, which means that the data type being operated is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods respectively. So how do you understand parameterized types? As the name suggests, the type is parameterized from the original specific type, similar to the variable parameters in the method. At this time, the type is also defined in the form of a parameter (which can be called a type parameter), and then the specific type is passed in when using/calling type (type argument).

It seems a bit complicated. First, let’s take a look at the above example using generic writing.

public class GenericTest {
    public static void main(String[] args) {
        /*
        List list = new ArrayList();
        list.add("qqyumidi");
        list.add("corn");
        list.add(100);
        */
        List<String> list = new ArrayList<String>();
        list.add("qqyumidi");
        list.add("corn");
        //list.add(100);   // 1  提示编译错误
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i); // 2
            System.out.println("name:" + name);
        }
    }
}
Copy after login

After adopting the generic writing method, a compilation error will occur when trying to add an Integer type object at //1. Through List, it is directly restricted that the list collection can only contain String type elements. , so there is no need to perform forced type conversion at //2, because at this time, the collection can remember the type information of the element, and the compiler can already confirm that it is a String type.

Combined with the above generic definition, we know that in List, String is a type actual parameter, that is to say, the corresponding List interface must contain type parameters. And the return result of the get() method is also directly the type of this formal parameter (that is, the corresponding incoming type actual parameter). Let’s take a look at the specific definition of the List interface:

public interface List<E> extends Collection<E> {
    int size();
    boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator();
    Object[] toArray();
    <T> T[] toArray(T[] a);
    boolean add(E e);
    boolean remove(Object o);
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean addAll(int index, Collection<? extends E> c);
    boolean removeAll(Collection<?> c);
    boolean retainAll(Collection<?> c);
    void clear();
    boolean equals(Object o);
    int hashCode();
    E get(int index);
    E set(int index, E element);
    void add(int index, E element);
    E remove(int index);
    int indexOf(Object o);
    int lastIndexOf(Object o);
    ListIterator<E> listIterator();
    ListIterator<E> listIterator(int index);
    List<E> subList(int fromIndex, int toIndex);
}
Copy after login

We can see that after adopting the generic definition in the List interface, the E in represents the type parameter and can receive specific Type arguments, and in this interface definition, wherever E appears, it represents the same type argument received from the outside.

Naturally, ArrayList is the implementation class of the List interface, and its definition form is:

public class ArrayList<E> extends AbstractList<E> 
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
    
    public E get(int index) {
        rangeCheck(index);
        checkForComodification();
        return ArrayList.this.elementData(offset + index);
    }
    
    //...省略掉其他具体的定义过程
}
Copy after login

From this, we understand from the source code perspective why there is a compilation error when adding an Integer type object at //1 , and the type obtained by get() at //2 is directly the String type.

3. What is the tuple class library and how to use it?

Why use tuple?

Tuples, like lists, may be used for data storage and contain multiple data; but different from lists: lists can only store the same data type, but tuples are different, they can store Different data types, such as int, string, list, etc., can be stored at the same time, and can be infinitely expanded according to needs.

For example, in web applications, a problem often encountered is data paging. Querying paging needs to contain several pieces of information: the current page number and page size; the query result returns data: the data record of the current page. , but if you need to display the current page, page size, total number of pages and other information in the foreground, you must have another information: the total number of data records, and then calculate the total number of pages and other information based on the above information. At this time, when querying a certain page of information, two data types need to be returned, one is list (current data record), and the other is int (total number of records). Of course, these two values ​​can be obtained in two methods and two database connections. In fact, when querying the list, the total number of records has been obtained through SQL query. If you open another method and make another database connection to query the total number of records, it will be a bit unnecessary, a waste of time, a waste of code, and a waste of life. Serious words~ In this case, we can use tuples to get the total number of records and the current page records in one database connection, and store them in it, simple and clear!

4. Customized generic interfaces, generic classes and generic methods

从上面的内容中,大家已经明白了泛型的具体运作过程。也知道了接口、类和方法也都可以使用泛型去定义,以及相应的使用。是的,在具体使用时,可以分为泛型接口、泛型类和泛型方法。

自定义泛型接口、泛型类和泛型方法与上述Java源码中的List、ArrayList类似。如下,我们看一个最简单的泛型类和方法定义:

public class GenericTest {
    public static void main(String[] args) {
        Box<String> name = new Box<String>("corn");
        System.out.println("name:" + name.getData());
    }
}
class Box<T> {
    private T data;
    public Box() {
    }
    public Box(T data) {
        this.data = data;
    }
    public T getData() {
        return data;
    }
}
Copy after login

在泛型接口、泛型类和泛型方法的定义过程中,我们常见的如T、E、K、V等形式的参数常用于表示泛型形参,由于接收来自外部使用时候传入的类型实参。那么对于不同传入的类型实参,生成的相应对象实例的类型是不是一样的呢?

public class GenericTest {
    public static void main(String[] args) {
        Box<String> name = new Box<String>("corn");
        Box<Integer> age = new Box<Integer>(712);
        System.out.println("name class:" + name.getClass());      // com.qqyumidi.Box
        System.out.println("age class:" + age.getClass());        // com.qqyumidi.Box
        System.out.println(name.getClass() == age.getClass());    // true
    }
}
Copy after login

由此,我们发现,在使用泛型类时,虽然传入了不同的泛型实参,但并没有真正意义上生成不同的类型,传入不同泛型实参的泛型类在内存上只有一个,即还是原来的最基本的类型(本实例中为Box),当然,在逻辑上我们可以理解成多个不同的泛型类型。

究其原因,在于Java中的泛型这一概念提出的目的,导致其只是作用于代码编译阶段,在编译过程中,对于正确检验泛型结果后,会将泛型的相关信息擦出,也就是说,成功编译过后的class文件中是不包含任何泛型信息的。泛型信息不会进入到运行时阶段。

五. 类型通配符

接着上面的结论,我们知道,Box和Box实际上都是Box类型,现在需要继续探讨一个问题,那么在逻辑上,类似于Box和Box是否可以看成具有父子关系的泛型类型呢?

为了弄清这个问题,我们继续看下下面这个例子:

public class GenericTest {
    public static void main(String[] args) {
        Box<Number> name = new Box<Number>(99);
        Box<Integer> age = new Box<Integer>(712);
        getData(name);
        
        //The method getData(Box<Number>) in the type GenericTest is 
        //not applicable for the arguments (Box<Integer>)
        getData(age);   // 1
    }
    
    public static void getData(Box<Number> data){
        System.out.println("data :" + data.getData());
    }
}
Copy after login

我们发现,在代码//1处出现了错误提示信息:The method getData(Box) in the t ype GenericTest is not applicable for the arguments (Box)。显然,通过提示信息,我们知道Box在逻辑上不能视为Box的父类。那么,原因何在呢?

public class GenericTest {
    public static void main(String[] args) {
        Box<Integer> a = new Box<Integer>(712);
        Box<Number> b = a;  // 1
        Box<Float> f = new Box<Float>(3.14f);
        b.setData(f);        // 2
    }
    public static void getData(Box<Number> data) {
        System.out.println("data :" + data.getData());
    }
}
class Box<T> {
    private T data;
    public Box() {
    }
    public Box(T data) {
        setData(data);
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}
Copy after login

这个例子中,显然//1和//2处肯定会出现错误提示的。在此我们可以使用反证法来进行说明。

假设Box在逻辑上可以视为Box的父类,那么//1和//2处将不会有错误提示了,那么问题就出来了,通过getData()方法取出数据时到底是什么类型呢?Integer? Float? 还是Number?且由于在编程过程中的顺序不可控性,导致在必要的时候必须要进行类型判断,且进行强制类型转换。显然,这与泛型的理念矛盾,因此,在逻辑上Box不能视为Box的父类。

好,那我们回过头来继续看“类型通配符”中的第一个例子,我们知道其具体的错误提示的深层次原因了。那么如何解决呢?总部能再定义一个新的函数吧。这和Java中的多态理念显然是违背的,因此,我们需要一个在逻辑上可以用来表示同时是Box和Box的父类的一个引用类型,由此,类型通配符应运而生。

类型通配符一般是使用 ? 代替具体的类型实参。注意了,此处是类型实参,而不是类型形参!且Box在逻辑上是Box、Box...等所有Box<具体类型实参>的父类。由此,我们依然可以定义泛型方法,来完成此类需求。

public class GenericTest {
    public static void main(String[] args) {
        Box<String> name = new Box<String>("corn");
        Box<Integer> age = new Box<Integer>(712);
        Box<Number> number = new Box<Number>(314);
        getData(name);
        getData(age);
        getData(number);
    }
    public static void getData(Box<?> data) {
        System.out.println("data :" + data.getData());
    }
}
Copy after login

有时候,我们还可能听到类型通配符上限和类型通配符下限。具体有是怎么样的呢?

在上面的例子中,如果需要定义一个功能类似于getData()的方法,但对类型实参又有进一步的限制:只能是Number类及其子类。此时,需要用到类型通配符上限。

public class GenericTest {
    public static void main(String[] args) {
        Box<String> name = new Box<String>("corn");
        Box<Integer> age = new Box<Integer>(712);
        Box<Number> number = new Box<Number>(314);
        getData(name);
        getData(age);
        getData(number);
        
        //getUpperNumberData(name); // 1
        getUpperNumberData(age);    // 2
        getUpperNumberData(number); // 3
    }
    public static void getData(Box<?> data) {
        System.out.println("data :" + data.getData());
    }
    
    public static void getUpperNumberData(Box<? extends Number> data){
        System.out.println("data :" + data.getData());
    }
}
Copy after login

此时,显然,在代码//1处调用将出现错误提示,而//2 //3处调用正常。

类型通配符上限通过形如Box形式定义,相对应的,类型通配符下限为Box形式,其含义与类型通配符上限正好相反

六. 怎么构建复杂模型如list元组?

泛型的一个重要好处是能够简单而安全地创建复杂的模型。如List元组。

package Generics;  
  
import java.util.ArrayList;  
  
class ThreeTuple2<A,B,C>{  
    public final A first;  
    public final B second;  
    private final C three;  
    public ThreeTuple2(A a,B b,C c){  
        first = a;  
        second = b;  
        three = c;  
    }  
    public String toString(){  
        return "(" + first + "," + second + "," + three + ")";  
    }  
}  
  
public class TupleList<A,B,C> extends ArrayList<ThreeTuple2<A,B,C>> {  
    static ThreeTuple2<Integer,String,Character> h(){  
        return new ThreeTuple2<Integer,String,Character>(99,"掌上洪城",&#39;a&#39;);  
    }  
    public static void main(String[] args) {  
        TupleList<Integer,String,Character> ts = new TupleList<Integer,String,Character>();  
        ts.add(h());  
        ts.add(h());  
        for(ThreeTuple2<Integer,String,Character> ttp:ts)  
        System.out.println(ttp);          
    }  
}  
package Generics;  
  
import java.util.ArrayList;  
  
class ThreeTuple2<A,B,C>{  
    public final A first;  
    public final B second;  
    private final C three;  
    public ThreeTuple2(A a,B b,C c){  
        first = a;  
        second = b;  
        three = c;  
    }  
    public String toString(){  
        return "(" + first + "," + second + "," + three + ")";  
    }  
}  
  
public class TupleList<A,B,C> extends ArrayList<ThreeTuple2<A,B,C>> {  
    static ThreeTuple2<Integer,String,Character> h(){  
        return new ThreeTuple2<Integer,String,Character>(99,"掌上洪城",&#39;a&#39;);  
    }  
    public static void main(String[] args) {  
        TupleList<Integer,String,Character> ts = new TupleList<Integer,String,Character>();  
        ts.add(h());  
        ts.add(h());  
        for(ThreeTuple2<Integer,String,Character> ttp:ts)  
        System.out.println(ttp);          
    }  
}  
/* 输出结果为: 
(99,掌上洪城,a) 
(99,掌上洪城,a) 
*/
Copy after login

七. 泛型的擦除

package generics;  
  
import java.util.*;  
  
public class ErasedTypeEquivalence {  
    public static void main(String[] args) {  
        Class c1 = new ArrayList<String>().getClass();  
        Class c2 = new ArrayList<Integer>().getClass();  
        System.out.println(c1 == c2);  
    }  
} /* 
     * Output: true 
     */// :~
Copy after login

在泛型内部,无法获得任何有关泛型参数类型的信息。

ArrayList和ArrayList是相同的类型。

擦除的补偿

要想在表达式中使用类型,需要显式地传递类型的class对象。

package generics;  
class Building {  
}  
  
class House extends Building {  
}  
  
public class ClassTypeCapture<T> {  
    Class<T> kind;  
  
    public ClassTypeCapture(Class<T> kind) {  
        this.kind = kind;  
    }  
  
    public boolean f(Object arg) {  
        return kind.isInstance(arg);  
    }  
  
    public static void main(String[] args) {  
        ClassTypeCapture<Building> ctt1 = new ClassTypeCapture<Building>(Building.class);  
        System.out.println(ctt1.f(new Building()));  
        System.out.println(ctt1.f(new House()));  
        ClassTypeCapture<House> ctt2 = new ClassTypeCapture<House>(House.class);  
        System.out.println(ctt2.f(new Building()));  
        System.out.println(ctt2.f(new House()));  
    }  
} /* 
     * Output: true true false true 
     */// :~
Copy after login

八. 可以创建泛型数组吗?相应的应用场景怎么处理?

正如你在下面示例Erased.java中所见,不能创建泛型数组。一般的解决方案是任何想要创建泛型数组的地方都使用ArrayList:

package generics;  
  
public class Erased<T> {  
    private final int SIZE = 100;  
  
    public static void f(Object arg) {  
        if (arg instanceof T) {  
        } // Cannot make a static reference to the non-static type T  
        T var = new T(); // Error  
        T[] array = new T[SIZE]; // Error  
        T[] array = (T) new Object[SIZE]; // Unchecked warning  
    }  
} /// :~
Copy after login

使用ArrayList示例

package generics;  
  
import java.util.*;  
  
public class ListOfGenerics<T> {  
    private List<T> array = new ArrayList<T>();  
  
    public void add(T item) {  
        array.add(item);  
    }  
  
    public T get(int index) {  
        return array.get(index);  
    }  
} /// :~
Copy after login

九. 泛型限定(上限和下限)的表达式是怎样的?

上限:?extends E:可以接收E类型或者E的子类型对象。

下限:?super E:可以接收E类型或者E的父类型对象。

上限什么时候用:往集合中添加元素时,既可以添加E类型对象,又可以添加E的子类型对象。为什么?因为取的时候,E类型既可以接收E类对象,又可以接收E的子类型对象。 

下限什么时候用:当从集合中获取元素进行操作的时候,可以用当前元素的类型接收,也可以用当前元素的父类型接收。

十. 什么时候用泛型?

当接口、类及方法中的操作的引用数据类型不确定的时候,以前用的Object来进行扩展的,现在可以用泛型来表示。这样可以避免强转的麻烦,而且将运行问题转移到的编译时期。

泛型的细节:

1)、泛型到底代表什么类型取决于调用者传入的类型,如果没传,默认是Object类型;

2)、使用带泛型的类创建对象时,等式两边指定的泛型必须一致;

    原因:编译器检查对象调用方法时只看变量,然而程序运行期间调用方法时就要考虑对象具体类型了;

3)、等式两边可以在任意一边使用泛型,在另一边不使用(考虑向后兼容);

ArrayListal = new ArrayList();  //错

//要保证左右两边的泛型具体类型一致就可以了,这样不容易出错。

ArrayList al = new ArrayList();

al.add("aa");  //错

//因为集合具体对象中既可存储String,也可以存储Object的其他子类,所以添加具体的类型对象不合适,类型检查会出现安全问题。 ?extendsObject 代表Object的子类型不确定,怎么能添加具体类型的对象呢?

public static voidmethod(ArrayList al) {

al.add("abc");  //错

  //只能对al集合中的元素调用Object类中的方法,具体子类型的方法都不能用,因为子类型不确定。

十一. Java类库中的泛型有那些?

所有的标准集合接口都是泛型化的—— Collection、List、Set 和 Map。类似地,集合接口的实现都是用相同类型参数泛型化的,所以HashMap 实现 Map 等。

除了集合类之外,Java 类库中还有几个其他的类也充当值的容器。这些类包括 WeakReference、SoftReference 和 ThreadLocal。





Related labels:
source:php.cn
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