Java 泛型


泛型:Generics,指在类定义时不指定类中信息的具体数据类型,而是用一个标识符来代替,
     当外部实例化对象时,来指定具体的数据类型。

    ---定义类或者定义接口时,不明确指定类中信息的具体数据类型,在实例化时再来指定,极大的提升了
         程序的扩展性。
     ---一个类可以装载各种不同的数据类型,泛型可以指代类中成员变量的类型,方法的返回值类型以及方法的参数类型


应用模式:

泛型上下限——
     void test(ArrayList<? extends Number> list){   
     }
    
     void test(ArrayList<? super String> list){   
     }

泛型类——
     public class A<T,S,W>{
         private T id;
         private S name;
         private W age;   
     }
    
泛型接口——
     public interface B<T>{
         public T getEle();
     }

// Number类对 6种包装类的父类
List list = new ArrayList<~>();
List<Number> list3 = new ArrayList<>();
* The abstract class {@code Number} is the superclass of platform
* classes
representing numeric values that are convertible to the
* primitive types {@code byte}, {@code double}, {@code float}, {@code
* int}, {@code long}, and {@code short}.


PriorityQueue中始终保障remove():

通过使用数组方式,存放二叉树结构,保障第一个内容始终为最小值。

//添加元素

private void siftUpUsingComparator(int k, E x) {
         while (k > 0) {
             int parent = (k - 1) >>> 1;
             Object e = queue[parent];
             if (comparator.compare(x, (E) e) >= 0)
                 break;
             queue[k] = e;
             k = parent;
         }
         queue[k] = x;
     }

//移除元素

@SuppressWarnings("unchecked")
     private void siftDownUsingComparator(int k, E x) {
         int half = size >>> 1;
         while (k < half) {
             int child = (k << 1) + 1;
             Object c = queue[child];
             int right = child + 1;
             if (right < size &&
                 comparator.compare((E) c, (E) queue[right]) > 0)
                 c = queue[child = right];
             if (comparator.compare(x, (E) c) <= 0)
                 break;
             queue[k] = c;
             k = child;
         }
         queue[k] = x;
     }

Math类:
     Math.sqrt()//平方根
     Math.cbrt()//立方根
     Math.random()//随机数
    
Random类
     int numb = new Random().nextInt(10);

原文地址:https://www.cnblogs.com/macro-renzhansheng/p/12539549.html