Description
A collection interface: 6 interfaces (short dashed lines) representing different collection types, which are the basis of the collection framework.
Abstract class: 5 abstract classes (long dashed lines), partial implementations of the collection interface. Can be extended to custom collection classes.
Implementation class: 8 implementation classes (solid line representations), specific implementations of the interfaces.
· The Collection interface is a set of objects that allow duplicates.
· The Set interface inherits Collection, but does not allow repetition, using one of its own internal arrangement mechanisms.
· The List interface inherits Collection, allowing repetition, placing elements in the order in which they are placed, and not rearranging them.
· The map interface is a pair of key-value objects that hold some key-value pairs. There can be no duplicates in map.
Key have their own internal arrangement mechanism.
· The element type in the container is object. When an element is obtained from a container, it must be converted to its original type.
I. Overview
The Java Collection Framework defines a series of classes that are containers for storing data. Compared to arrays, StringBuffer (StringBuilder), it is characterized by:
1. For storing objects
2. Variable Set length
3. Basic data types cannot be stored
second, collection interface
Both the set interface and the list interface implement the collection interface, so it is obvious that the collection interface holds the common content of the set interface and the list interface.
Methods in the collection interface:
PackageP01. Basecollectiondemo;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator; Public classCollectionDemo01 { Public Static voidMain (String args[]) {//Show1 (); //Show2 (); //show3 (); //show4 ();show5 (); } Private Static voidshow5 () {/** Demonstrate Retainall, toarray methods*/Collection Coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); System.out.println (coll); Collection coll1=NewArrayList (); Coll1.add ("ABC1"); Coll1.add ("ABC3"); Coll1.add ("ABC4"); Coll1.add ("Abc8"); System.out.println (COLL1); System.out.println (Coll.retainall (COLL1)); System.out.println (coll);//contrary to RemoveAll, only the same parts are taken } Private Static voidshow4 () {/** Demo size, Iterator*/Collection Coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); System.out.println (coll); System.out.println (Coll.size ()); for(Iterator it=coll.iterator (); It.hasnext ();) {System.out.println (It.next ()); } } Private Static voidshow3 () {/** Demonstrate contains, Containsall, IsEmpty methods*/Collection Coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); System.out.println (coll); Collection coll1=NewArrayList (); Coll1.add ("Abc5"); Coll1.add ("Abc6"); Coll1.add ("ABC7"); Coll1.add ("Abc8"); System.out.println (COLL1); Coll.addall (COLL1); System.out.println (coll); System.out.println (Coll.containsall (COLL1)); System.out.println (Coll.contains ("ABC1")); Coll.remove ("ABC1"); System.out.println (Coll.contains ("ABC1")); Coll.clear (); System.out.println (Coll.isempty ()); } Private Static voidShow2 () {/** Demo Remove, RemoveAll, clear Method*/Collection Coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); System.out.println (coll); Collection coll1=NewArrayList (); Coll1.add ("Abc5"); Coll1.add ("Abc6"); Coll1.add ("ABC7"); Coll1.add ("Abc8"); System.out.println (COLL1); Coll.addall (COLL1); System.out.println (coll); Coll.removeall (COLL1); System.out.println (coll); Coll.remove ("ABC1"); System.out.println (coll); Coll.clear (); System.out.println ("Print collection elements:" +Coll); } Public Static voidShow1 () {/** Demo Add, AddAll*/Collection Coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); System.out.println (coll); Collection coll1=NewArrayList (); Coll1.add ("Abc5"); Coll1.add ("Abc6"); Coll1.add ("ABC7"); Coll1.add ("Abc8"); System.out.println (COLL1); Coll.addall (COLL1); System.out.println (coll); }}
Three, iterators
1. Traverse
There is a very important interface iterator in the framework, which can be used to traverse all the other containers in the framework.
The iterator functionality in Java is relatively simple and can only be moved one way:
(1) Use method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence. Note: The iterator () method is an Java.lang.Iterable interface that is inherited by collection.
(2) Use Next () to get the next element in the sequence.
(3) Use Hasnext () to check if there are elements in the sequence.
(4) use remove () to delete the newly returned element of the iterator.
Iterator is the simplest implementation of Java iterators, with more functionality for the listiterator of list design, which can traverse the list in two directions or insert and delete elements from a list.
The two types of traversal are similar, but somewhat different, the first is recommended.
PackageP01. Basecollectiondemo;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator; Public classIteratordemo { Public Static voidMain (string[] args) {Collection coll=NewArrayList (); Coll.add ("ABC1"); Coll.add ("ABC2"); Coll.add ("ABC3"); Coll.add ("ABC4"); Demo01 (coll); Demo02 (coll); } Private Static voidDemo02 (Collection coll) {Iterator it=Coll.iterator (); while(It.hasnext ()) {System.out.print (It.next () )+" "); } System.out.println (); } Private Static voidDemo01 (Collection coll) { for(Iterator it=coll.iterator (); It.hasnext ();) {System.out.print (It.next ()+" "); } System.out.println (); }}
View Code
Analysis: The first way to iterate through a collection is that the iterator object is cleared in memory as the For loop ends, while the second method achieves the traversal effect, but the memory occupied by the iterator after the loop is not freed, and a portion of the memory is wasted
2. Iterative Principles
Since the data structures within each container may be different, how can you achieve the purpose of traversing a variety of containers with only one interface?
Analysis: The inside of the container maintains an object that implements the iterator interface, and each object operates within the container (they operate only members within this class), but provides the same traversal method: Hasnext and Next, with these two methods, all the objects within the container can be obtained. This means that the iterator object must depend on the specific container, because each container has a different data structure. For a container user, a specific implementation is not important, as long as the object is obtained through the container to the iterator of that implementation, which is the iterator method. The iterator interface is the common interface for extracting elements from all collection containers.
Java Collection Framework