First part of the collection framework introduction
The Java platform provides a new collection framework. The "Collection Framework" is mainly composed of a set of interfaces used to operate objects. Different interfaces describe a set of different data types.
Java 2 Collection Frame
Collection interface: 6 interfaces (short dashed lines) indicating that different collection types are the basis of the collection framework.
Abstract class: 5 abstract classes (long dashed lines), implementation of partial integration interfaces. Scalable to custom set classes.
Implementation class: 8 implementation classes (solid line representation), specific implementation of the interface.
To a large extent, once you understand the interface, you understand the framework. Although you always create interface-specific implementations, access to the actual collection should limit the use of the interface method; therefore, allow you to change the basic data structure without having to change the other code.
· The Collection interface is a group of allowed repetitive objects.
• The SET interface inherits the Collection, but does not allow repeated, using one of its interiors.
· The List interface inherits the Collection, allows you to repeat, place the element to place the element, and will not rearrange it.
• The MAP interface is a key-value object that is held, that is, the HEY-VALUE PAIRS. There is no repetition in MAP. Have your own internal arrangement mechanism.
• The element type in the container is Object. When an element is obtained from a container, it must be converted into the original type.
Java 2 Simplified Collection Frame
Part II Collection Interface
COLLECTION interface
Used to represent any object or element group. Use this interface when you want to handle a set of elements as much as possible.
(1) Single element added, delete operation:
Boolean Add (Object O): Add object to a collection
Boolean Remove (Object O): Delete the object O if there is an object with the O, if there is an object with the O
(2) Query operation:
INT size (): Returns the number of elements in the current collection
Boolean isempty (): Is there any elements in the collection
Boolean Contains (Object O): Whether to find a collection of objects O
ITerator iterator (): Returns an iterator to access the individual elements in the collection
(3) Group operation: acting on element group or whole collection
Boolean ContainSall (Collection C): Find all elements in the collection C
Boolean Add (Collection C): Add all elements in the collection C to the collection
Void clear (): Delete all elements in the collection
Void Removeall (Collection C): Remove all elements in the collection C from the collection
Void RetainAll (Collection C): Remove the elements that do not contain in the collection C
(4) Collection converts to the Object array:
Object [] toarray (): Returns an Array with a collection of all elements
Object [] toArray (Object [] a): Returns an Array containing all elements of the set. The model returned by the runtime is the same, and the parameter A needs to be converted to the correct type.
In addition, you can convert a collection into any other object array. However, you cannot directly convert the collection into an array of basic data types because the collection must hold objects.
"The beverage interface method is optional. Because an interface implementation must implement all interface methods, the calling program requires a way to know that an optional method is not supported. If an option is called, a UNSUPPORTEDOPERATIONException is thrown, and the operation failed because the method is not supported. This exception class inherits the RuntimeException class to avoid placing all collections into the try-catch block. Collection does not provide a get () method. If you want to traverse the elements in Collectin, you must use item.
1.1.AbstractCollection abstraction
The AbstractCollection class provides the basic functionality of the specific "Collection Framework" class. Although you can implement all methods of the Collection interface, other methods are implemented by the AbstractCollection class in addition to the Iterator () and Size () methods implementation in the appropriate subclass. If the subclass is not overwritable, the options such as add () will throw an exception.
1.2.iterator interface
The Iterator () method of the Collection interface returns an Iterator. The Iterator interface method can access the individual elements in the collection one by one by one, and securely remove the appropriate elements from the Collection.
(1) Boolean Hasnext (): Judging whether there is another accessible element
Object next (): Returns the next element to be accessed. If the end of the collection is reached, the NosuchelementException is thrown.
(2) Void remove (): Delete the object returned last time. This method must be followed after access to an element. If the collection has been modified last time, the method will throw an IllegalStateException.
"Deleting operations in Iterator also have an impact on the underlying Collection. "
The iterator is fault-fast. This means that when another thread modifies the underlying collection, if you are using the Iterator traversal collection, Iterator will throw ConcurrentModificationException (another RuntimeException exception) exception and immediately failed.
2.List interface
The LIST interface inherits the Collection interface to define an ordered collection that allows the repetition. This interface can not only process a part of the list, but also add a positional operation.
(1) The facing operation includes the function of inserting an element or collection, further comprising the function of acquiring, removing or changing elements. Searching elements in list can start from the head or tail of the list, if an element is found, the location of the elements are also reported:
Void Add (INT INDEX, Object Element): Add an element ELEMENT on the specified location INDEX
Boolean Addall (INDEX, Collection C): Add all elements of the collection C to the specified location Index
Object get (int index): Returns the element of the specified location in the list
INT INDEXOF (Object O): Returns the position of the first element O, otherwise returns -1
INT LastIndexof (Object O): Returns the location of the last element O, otherwise returns -1
Object Remove (int index): Delete elements on the specified location
Object Set (int index, object element): replaces the elements on Index with element Element and return old elements
(2) The list interface is not only used in the entire list, but also handles a subset of the collection: listiterator listiterator (): Returns a list iterator to access the elements in the list
Listiterator ListIterator (int index): Returns a list iterator to start accessing the elements in the list from the specified location index
List Sublist (int fromindex, int toindex): Returns a list view from the specified location fromNDEX (included) to the toIndex (not included)
"Changes to sub-lists (such as add (), remove (), and set () calls also have an effect on the underlying List. "
2.1.Listiterator interface
The ListIterator interface inherits the Iterator interface to support the elements in the underlying collection, and support two-way access. Listiterator does not have a current location, between the cursor is between the values returned by the previous and next method. A list of a length N, N 1 effective index value:
(1) Void Add (Object O): Add the object o to the current location
Void Set (Object O): Use the object O to replace the previous element accessed by the next or Previous method. If the list structure is modified last time, ILLEGALSTATEEXCEPTION will throw an unlegation.
(2) Boolean Hasprevious (): During the judgment, there is an element to be accessed when it is iterated.
Object previous (): Return to the previous object
INT nextIndex (): Returns the index of the elements that will be returned next time
INT PreviousIndex ():
Return to the next call
Previous
When the method, the index of the returned element is normal, without the Listiterator to change the direction of the traversal set element - forward or backward. Although it is technically implemented, new () immediately calls next (), and returns the same element. The order of calling next () and previous () is reversed, the result is the same. "
"We also need to explain the add () operation again. Adding an element can cause new elements to be immediately added to the front of the implicit cursor. Therefore, the addition of the element to call previous () will return new elements, and NEXT () does not work, returns the next element before adding operation. "