java Collection Framework - Summary of Interface Relations

Keywords: Java SDK

Collection has two main interfaces: Collection (the root interface of a collection) and Map (the root interface of a mapping set).

 1.Collection: From the Java.util package, the Java SDK does not provide classes directly inherited from Collection. The classes provided by the Java SDK are "children" inherited from Collection.  
                       Interfaces such as List and Set.
          List: An ordered Collection containing repetitive elements
                 1.1.1. You can use indexes (like subscripts in arrays) to access elements at the specified location of the List.
                                list.get(i)// Get the elements in the List collection
                 1.1.2. Allow the same elements and null elements in a collection
                 1.1.3. The common classes that implement the List interface are LinkedList, ArrayList, Vector and Stack.   
                 1.1.4. High efficiency of element searching
          1.2.Set: An unordered Collection that does not contain duplicate elements
                 1.2.1. There is at most one null element 
                 1.2.2. The incoming Collection parameter cannot contain duplicate elements, which will be overwritten if existing elements are added.
                 1.2.3. The main implementation classes are HashSet and TreeSet. 
                 1.2.4. Delete and insert elements efficiently      
          1.3.Queue: Used to simulate the data structure of queues, FIFO
                 1.3.1. LinkedList for Implementing Interface Implementation Classes     
                 1.3.2.LinkedList can be used as a List collection, double-ended queue, stack  
  2.Map: From the Java.util package, Map does not inherit the Collection interface. Map provides key-to-value mapping
          2.1. A Map cannot contain the same key, and each key can only map to a value.
          2.2. The Map interface provides views of three sets. The content of the Map can be treated as a set of keys, a set of values, or a set of key-value mappings.
          2.3. Common classes that implement the Map interface are Hashtable (synchronous), HashMap (asynchronous), WeakHashMap.
                WeakHashMap is an improved HashMap, which implements a "weak reference" to key. If a key is no longer referenced externally, the key can be reclaimed by GC.

Summary: The difference between Set and List (3 points)

     1. Set Interface instances store disordered, non-repetitive data. List Interface instances store orderly, repeatable elements.
     2. Set The retrieval efficiency is low, the deletion and insertion efficiency is high, the insertion and deletion will not cause the element position change. <Implementation class HashSet,TreeSet>. 
     3. List Similar to arrays, it can grow dynamically and automatically according to the length of the actual stored data. List The length. Finding elements is efficient and inserting and deleting elements is inefficient, because it causes other elements to change their positions. <Implementation class ArrayList,LinkedList,Vector> .   
//Extension: Iterators (simply for counting!!!!)
      1.Traversal Set Method (3)
            1.1.Generally traversal arrays are used for Circulation or enhancement for,These two methods can also be used in collection frameworks
            1.2.The iterator is used to traverse the set framework, which is an object and is implemented. Iterator Interface or ListIterator Interface.
      2.Iterator ( Iterator),It allows you to get or delete elements of a collection through loops.
      3.ListIterator Inherited Iterator,To allow bidirectional traversal of lists and modification of elements      
      4.ListIterator and Iterator Difference:
          4.1.Difference
               4.1.1.Iterator It's more general. List,Set,Map All can be used. ListIterator Can only be used List Collection
               4.1.2.ListIterator Yes add()The method can be used to solve the problem of ___________ List To add objects, and Iterator cannot
               4.1.3.ListIterator Yes hasPrevious()and previous()Method: Reverse (sequential forward) traversal can be realized. Iterator You can't do that.
               4.1.4.ListIterator You can locate the current index location. nextIndex()and previousIndex()It can be achieved. Iterator No such function
               4.1.5.ListIterator yes Iterator Subinterface, add some methods
               4.1.6.ListIterator Object modification can be achieved. set()The method can be realized. Iierator It can only be traversed, not modified.
           4.2.Same points
               4.2.1.Delete objects can be implemented 
               4.2.2. ListIterator and Iterator Both have hasNext()and next()The method can realize sequential backward traversal.
                     4.2.2.1.hasNext():If there are elements in a collection, it returns true
                     4.2.2.2.next():Next element in a collection
           4.3.Conclusion:
              4.3.1.ListIterator It's a ratio. Iterator More powerful iteration tools      
              4.3.2.ListIterator The only drawback is that it can only be used List aggregate
              4.3.3.In a single-threaded traversal process, if you want to remove Operations that can call the iterator's remove Method rather than collection class remove Method
       5.Common code examples:
            5.1.Add elements to the collection, using ListIterator Of add() Method
                  ArrayList<String> list = new ArrayList<>();
                             list.add("aaa");
                             list.add("bbb");
                             list.add("ccc");
                          System.out.println(list);//Get the set of elements [aaa,bbb,ccc]
                          System.out.println(list.get(0));//Get the element with subscript 0 [aaa]
                           //Iterator interface (no add() method)
                          //Iterator<String> it = list.iterator();
                            ListIterator<String> it = list.listIterator();  
                                   while(it.hasNext()){
	                              String s = it.next();
	                              //Add to the specified element (if not specified, add "1" after each element)
	                              if(s.equals("aaa")){
	                             	it.add("1");
	                     }
                 }
                        System.out.println(list);//[aaa,1,bbb,ccc]
                 }   
                 
           5.2.Delete elements in a collection( ListIterator and Iterator Yes.    
                           //Iterator interface (no add() method)
                          //Iterator<String> it = list.iterator();
                            ListIterator<String> it = list.listIterator();  
                                   while(it.hasNext()){
	                              String s = it.next();
	                              //Delete set element aaa
	                              if("aaa".equals(s)){
	                             	it.remove();
	                     }
                 }
                        System.out.println(list);//Output Removed Element Set [bbb,ccc]
                 }   
          5.3.Reset the elements in the collection, using ListIterator Of set() Method
                          //Iterator interface (no set() method)
                          //Iterator<String> it = list.iterator();
                            ListIterator<String> it = list.listIterator();  
                                   while(it.hasNext()){
	                              String s = it.next();
	                              //Set the set element AAA to AAA
	                              if("aaa".equals(s)){
	                             	it.set("AAA");
	                     }
                 }
                        System.out.println(list);//Output Removed Element Set [AAA,bbb,ccc]
                 }   
            
           //The next step is to introduce one by one the implementation classes in the collection interface (usage, distinction, etc.).              

Posted by The Silent on Tue, 07 May 2019 13:45:38 -0700