Home > Java > javaTutorial > How to use wildcards to enhance Java's generic performance

How to use wildcards to enhance Java's generic performance

王林
Release: 2023-04-23 08:04:20
forward
958 people have browsed it

    Use wildcards to enhance generics

    1. Question

    Generics are an important feature of JAVA. Using generic programming, you can Code reuse rate is improved.

    Implementation: Use wildcards in generic methods

    2. Problem-solving ideas

    Create a class: WildcardsTest.

    Create a method getMiddle() for getting the middle value of a given list.

    In generics, use "?" as a wildcard character. The use of wildcard characters is similar to that of ordinary type parameters. For example, wildcard characters can use the extends keyword to set the upper limit of the value. For example,

    extends Number>

    indicates that Byte, Double, Float, and Integer are suitable for this type parameter.

    Also, there is an upper limit and a lower limit, such as

    super Number>

    w means that the type parameter is the parent of the Number class Class, such as Object.

    3. Detailed code explanation

    package com.xiaoxuzhu;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    /**
     * Description:
     *
     * @author xiaoxuzhu
     * @version 1.0
     *
     * <pre class="brush:php;toolbar:false">
     * 修改记录:
     * 修改后版本	        修改人		修改日期			修改内容
     * 2022/5/10.1	    xiaoxuzhu		2022/5/10		    Create
     * 
    Copy after login
    * @date 2022/5/10 */ public class WildcardsTest { public static Object getMiddle(List list) { return list.get(list.size() / 2); } public static void main(String[] args) { List ints = new ArrayList(); ints.add(1); ints.add(2); ints.add(3); System.out.print("整型列表的元素:"); System.out.println(Arrays.toString(ints.toArray())); System.out.println("整型列表的中间数:" + getMiddle(ints)); List doubles = new ArrayList(); doubles.add(1.1); doubles.add(2.2); doubles.add(3.3); System.out.print("浮点列表的元素:"); System.out.println(Arrays.toString(doubles.toArray())); System.out.println("浮点列表的中间数:" + getMiddle(doubles)); } }

    How to use wildcards to enhance Javas generic performance

    Supplementary knowledge points

    Generics

    You can use , , , etc. to declare generics. The declaration method of limits the scope of T, and T can only be a subclass of User.

    1. Parameter types are used in the creation of classes, generic classes.

    2. Parameter types are used in the creation of interfaces, generic interfaces.

    3. Parameter types are used in the creation of methods, generic methods. Note the declaration position of the generic , after the method's modifier and before the return value type.

    ? Type wildcard character

    Several forms of wildcard character:

    1. Unlimited wildcard character, .

    2. Upper limit wildcard, . Indicates that the parameter type can only be a subclass of User.

    3. Lower limit wildcard, . Indicates that the parameter type can only be the parent class of User.

    public class P<T> {
      private T t;
      public T getT(){
        return t;
      }
    
      //通配符
      //设置指定类型的范围,超过范围就会报错
      //extends : 指定范围必须是其(这里是List)子类
      public void set(P<? extends List> list){
        return;
      }
    
      //super : 指定类型必须是其(这里是List)父类
      public void setSuper(P<? super List> list){
        return;
      }
    
      public static void main(String[] args) {
        new P<String>(); //T会被替换成String
        new P<Integer>(); //T会被替换成Integer
        Person<String,List,Double,User> person = new Person<String,List,Double,User>();//T会被替换成String,B会被替换成List...
        person.eat("鱼");
      }
    }
    class User<T extends P>{//这里给类的泛型指定范围,T必须要继承类P。
    
    }
    //一个类可以同时指定多个泛型
    class Person<T,B,S,N>{
      private T t;
      private B b;
      private S s;
      private N n;
      public void eat(T t){
        this.t = t;
        System.out.println("我正在吃"+this.t);
      }
    }
    Copy after login

    The declaration of generics indicates that an unknown data type is to be used in the creation of classes, interfaces, and methods. It can be of type Integer or String. Just define its type as T, S or N, etc.

    When instantiating, you must declare what type T is.

    When defining the usage method of a generic object, it is not known what type T is. It may be a String type or an Integer type. If T is defined as a certain generic data type, the parameters can only be of this data type. At this time, wildcards are used instead of certain generic data types.

    Use generics and wildcards to improve code reusability.

    If an object is divided into two parts: declaration and use. Generics focus on code reuse in type declaration, while wildcards focus on code reuse in usage. Generics are used to define uncertainty about internal data types, and wildcards are used to define uncertainty about the type of objects used.

    The above is the detailed content of How to use wildcards to enhance Java's generic performance. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    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