Java Collections Framework Overview (two) - a set of generic tools and Collections

java Collections Framework Overview (a) - List those things interface

Prospects Review

 This is a class about the ArrayList, not difficult to find, a collection inside the store five different types, it is clear that there is no specification, resulting in the collection can add any type of object. So, when printing doublr type of error. Display, which is an example of the type of insecurity.

public class TestArrayList {
    public static void main(String[] args){
        ArrayList list = new ArrayList();
        list.add(10);
        list.add(10.0);
        list.add("Hello");
        list.add('A');
        list.add(true);

        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += (double)list.get(i);
        }
    }
}

Exception in thread "main" java.lang.ClassCastException:

java.lang.Integer can not be cast to java.lang.Doubleat list.TestArrayList.main (TestArrayList.java:16) Therefore, in order to better regulate the array, the introduction of the pan type


Generic collections

Concept: parameterized types, type-safe collection, forced collection element types must be consistent.
Features:

  • You can check at compile time, rather than runtime exception is thrown
  • Access time. Do not have to type conversion (unboxing)
  • Different generic references can not be assigned to each other, the absence of generic polymorphic

The basic generic

ArrayList<Double> list = new ArrayList<Double>();
Here Insert Picture Description
List of other kinds of interfaces or implementation of the List is similar to the definition of generic.

Advanced Generics

Example 1. Generic

  • Generic classes: generic definition of new data types when the object
public class AdvancedGeneric {
    public static void main(String[] args){
        //在new对象的时候定义泛型的数据类型
        MyClass<Integer> myClass = new MyClass<Integer>();
        myClass.classMethod(12);
    }
}
//类中的泛型
class MyClass<E>{
    public void classMethod(E e){

    }
}

2. Generic Interface

  • Generic interface: implement class generic data types
public class AdvancedGeneric {
    public static void main(String[] args){
        //泛型接口
        MyImplClass myImplClass = new MyImplClass();
        myImplClass.interfaceMethod(true);
    }
}
//接口中的泛型
interface MyInterface<T>{
    void interfaceMethod(T t);
}
//实现接口的类 此处定义泛型的数据类型
class MyImplClass implements MyInterface<Boolean>{

    @Override
    public void interfaceMethod(Boolean b) {
    }
}

3. Static Generics

  • Static class generic: define a generic data types in a static method
public class AdvancedGeneric {
    public static void main(String[] args){
       //调用静态类
        MyStaticClass myStaticClass  = new MyStaticClass();
        myStaticClass.method(123);
        myStaticClass.method(true);
    }
//静态类:在静态方法中定义泛型的数据类型
class MyStaticClass {
    public static<T> void method(T t){

    }
}

But it seems Static in the generic object is no limitation on the type of data, if we need to pass numberthe value of the data type of the how to do it?

public class AdvancedGeneric {
    public static void main(String[] args){
       //调用静态类
        MyStaticClass myStaticClass  = new MyStaticClass();
        myStaticClass.method(123);
      //  myStaticClass.method(true); //类型错误
    }
//静态类
class MyStaticClass {
    public static<T extends Number> void method(T t){
        System.out.println(t);
    }
}

Upper bound wildcards: <? extends T>represents the upper bound of the type (including itself), thus wild type may be parameterized Tor Tsubclass.
Lower bound wildcard: <? super T>represents the parameterized type is a supertype of T (including itself), the layers of first, until the Object

Collections Tools

  • Set of tools: defines a set of common access method other than
method description
public static void reverse(List<?> list) Reversing the order of the elements in a set of
public static void shuffle(List<?> list) Reset random sequence set elements
public static void sort(List<?> list) Ascending order (element must implement Comparable interface)
  • Injection sortmethod in the argument Tmust implement Comparablethe interface
//使用sort方法必须实现Comparable接口
  public static <T extends Comparable<? super T>> void sort(List<T> list) {
        list.sort(null);
    } 

Example of use:

public class testCollections {
    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(5);
        list.add(7);
        list.add(9);
       // Collections.reverse(list);
        //Collections.shuffle(list);
        Collections.sort(list);
        for(Integer integer : list){
            System.out.println("运行结果:"+integer);
        }
    }
}
----sort------------  -------reverse----------------shuffle----------
运行结果:1             	运行结果:9				运行结果:7
运行结果:3				运行结果:7				运行结果:9
运行结果:5				运行结果:5				运行结果:1
运行结果:7				运行结果:3				运行结果:5
运行结果:9				运行结果:1				运行结果:3

Published 82 original articles · won praise 124 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_44717317/article/details/104640253