Collection List, Comparison, and Generics

1. Collection framework:
    1: Collection framework  
         1) Collection is used to store data, which is more convenient than array;
         2) Different data can be stored, but it is not recommended, so use generics to constrain;
         3) Use generics, benefit: compiler You can check whether the element types match to
                            avoid errors during operation; 2:         Commonly used sub-interfaces of the
     parent interface Collection             1) List: ordered, repeatable (call equals comparison between elements)              2) Set: unordered, not repeatable ( Calling equals comparison between elements)      3: Implementation class of List sub-interface         1) ArrayList:              a dynamic array inside, fast search speed, slow addition and deletion          2) LinkedList:               a linked list inside, slow search speed, fast addition and deletion 2. Detailed explanation: 1:       Common methods     of parent interface Collection :          1) add(Object e): add element          2) remove(Object obj): remove          3) addAll(Collection c): merge two collections














         4) removeAll(Collection c): remove the intersection of two sets
         5) retainAll(Collection c): retain the intersection
         6) clear(): clear the set
         7) isEmpty(): determine whether it is empty
         8) size(): return The number of set elements.
         9) toArray(): Convert the collection object into an array
     2: List sub-interface ---- Linear table Set
        commonly used implementation class:
             1) ArrayList:
                 It is a dynamic array encapsulated inside a linear structure. Length increases and decreases have been encapsulated.
                 Can be understood as automatic.
                 Fast search, slow additions and deletions,
                 unsafe threads, high efficiency
            2) LinkedList:
                               It is a linked list structure. An element stores the address of the previous element and the address of the next element.
                              Connected together like a chain.
                Increase delete fast, search slow            
                3) Vector: thread safety, low efficiency.
    3. Methods provided by the List interface:
        1) add(int index, Object obj): insert element
         2) add(Object obj);
         3) get(int index): get element
         4) remove(int index)
         5) int indexOf(Object obj): return element Subscript, return -1 to indicate that there is no such element
         6)lastIndexOf(Object obj):
             7)contains(Object obj)--boolean: determine whether the element obj is included
         8)toArry(): convert the collection into an array -
            note: use Object[] received,
              String[] array is of Object type
                  Object[] array converted to String[] will cause a
                  class casting exception ClassCastException
             9) Arrays.asList(str): Convert the array str into a collection, the converted collection cannot be added or deleted
            otherwise An exception will occur.
        10) subList(int fromIndex,int toIndex);: is to get the sub-collection of the collection.

        The collection object stores the address information of these elements

    4.: Enhance the for loop, which is used to traverse a collection or an array.
        It is another way of writing iteration, not a new language.
         The compiler will compile this loop into iterative notation.
        Structure:
              for(element type variable name: variable of collection or array){
                    //
             }
        /*Also: use iterative traversal
         * Iterator, which is an interface, is used
         to traverse collection elements with *. The List interface provides the iterator method
         * to get the iterator implementation class object.
        /
        reg:
            //Use an iterator to traverse the collection a
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object obj = it.next();//Get the next object    
            if((Integer)obj ==6){
                it.remove();
                //Never use list.remove(6);
            }
            System.out.println(obj);
        }
        5.: Generics mechanism.
                 5.1: The essence of the generics mechanism is parameterized types.
                         During the definition of classes, interfaces and methods, parameter type names can be specified.
                         In the collection framework, all collection types use the generic mechanism.
            The advantage: After parameterizing the type, you can limit
               the type of the collection element, and the compiler will check it through the specified generic type.
               Reduce code errors at runtime.
                public class ArrayList<E>{
                     ………………
                     public boolean add(E e);
                 public E get(int index);
                ………………
                           }
         ArrayList uses the generic mechanism, and E is the type parameter.
          In specific use, you have to assign a value to E, and the assigned value is the type name.
        List<String> list = new ArrayList<String>();
       Note: The generic mechanism cannot use the eight basic data types, only reference types

       6.: Collection storage:
         Collection elements are all reference types. The memory of the collection object is the address information of the element,
        not the element itself.
      7.: Collection tool class: Collections (note the difference with Collection-)
            Method: sort(list): Sort the collection;
                    Collections.sort(list);// Sort the list object directly;
        summary:
            1) The collection has sort () method, indicating that the collection elements are sortable;
            2) How to implement the sort() method? --Method 1
                1. Implement the Comparable interface;
                2. Rewrite the compareTo(Object obj) method;
             3) compareTo(Object obj) method: It is used to compare elements.
                   this>obj, return the number of >0
                   this<obj, return the number of <0
                   this=obj, return the number of 0
            4) call sort(p1); method    
            5) Comparable: The interface also has a generic type, and the parameter passed is the implementation class type;
    8) The role and difference between Comparable and Comparator
           Comparable(adj.)---compareTo(Object a)
           Comparator(n.)---compare( Object a, Object b)
        1) Function: both are used for sorting; 2) Comparator:                 Comparator interface used
        when temporarily changing the sorting rules on the original sorting rules .
How to call the sort(p1, p2) method
        3) of the collection?
            1. Implement the Comparator interface;
            2. Rewrite the compare(Object ob1, Object ob2) method;
        4) Call sort(Collection coll, Comparator com);
        5) Use: Collections.sort(coll, com);
 for example:
        List<Cell > list = new ArrayList<Cell>();
        list.add(new Cell(1,2));
        Collections.sort(list);

        Use the Comparator interface to implement sorting:
        Comparator my = new MyComparator(); --method 1
            Collections.sort(list, my);

          /*
         * Use anonymous inner class writing to create a comparator object
         */
        Comparator<Point> my = new Comparator<Point>(){ --Method 2
            public int compare(Point p1, Point p2) {
                int dis1 = p1.getX()*p1.getX()+p1.getY()*p1.getY();
                int dis2 = p2.getX() *p2.getX()+p2.getY()*p2.getY();
                return dis2-dis1;
            }
        };
        Collections.sort(list,my);
        System.out.println(list);
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325655881&siteId=291194637