Java learning about the basic interface--collection interface for a collection framework

Source: Internet
Author: User

  The collection framework (Collection framework) is one of the most powerful subsystems in Java and is located in the Java.util package. The collection framework is a complex interface and class hierarchy that provides the latest technology for managing object groups. The Java Collection Framework standardizes the way programs work with groups of objects.

The collection framework needs to meet several goals in design. First, the framework must be high-performance. The implementation of basic collections (dynamic arrays, linked lists, trees, and hash tables) is efficient. It is seldom necessary to manually write one of these data engines. Second, the framework must allow different types of collections to work in a similar way and have a high degree of interoperability. Again, expansion or transformation must be easy to implement. To meet these goals, the entire collection framework is built on a standard set of interfaces, providing some standard implementations of these interfaces that can be used directly (such as LinkedList, HashSet, and TreeSet). As an option, you can also implement your own collection. For convenience, provide a variety of specific purposes for the implementation. Finally, you must add a mechanism that can integrate standard arrays into the collection framework.

The algorithm is another important part of the aggregation mechanism. The algorithm operates a collection and is defined as a static method in the Collections class. Therefore, all collections can use them.

Another content that is closely related to the collection framework is the Iterator interface. Iterators provide a common, standard way to access the elements in a collection, one element at a time. Because iterators are provided for each collection, the elements of all collection classes can be accessed through iterator defined methods.

The collection interface is the basis for building a collection framework because all classes that define the collection must implement that interface, collection is a generic interface

Collection<e> extends Iterable<e>
where e Specifies the type of object stored in the collection. The collection interface extends the Iterator interface.
Method Description
boolean  add(E e)
Ensures that this collection contains the specified element (optional action).
boolean addAll(Collection<? extends E> c)
Adds all the elements from the specified collection to this collection (optional operation).
void  clear()
Removes all elements from this collection (optional action).
boolean   contains(Object o)
Returns True if this collection contains the specified element.
boolean   containsAll(Collection<?> c)
Returns True if this collection contains all the elements in the specified collection .
boolean   equals(Object o)
Compares the specified object to this collection to obtain equality.
int    hashCode()
Returns the hash code value for this collection.
boolean    isEmpty()
Returns True if this collection does not contain elements.
boolean   remove(Object o)                                 
Removes a single instance of the specified element (if one exists) from the collection (optional action). Remove returns True
boolean    removeAll(Collection<?> c)
Removes all elements of this collection that are contained in the specified collection (optional operation).
default boolean removeIf(Predicate<? super E> filter)
Deletes all elements of this collection that satisfy the given predicate.
boolean    retainAll(Collection<?> c)
Preserves only the elements in this collection that are contained in the specified collection (optional operations).
int     size()
Returns the number of elements in this collection.
Object[]    toArray()                                            
Returns an array that contains all the elements in this collection.
<T> T[]    toArray(T[] a)
Returns an array containing all the elements in this collection; The run-time type of the returned array is the run-time type of the specified array.
Method details
    • ToArray
Object[] ToArray ()
Returns an array that contains all the elements in this collection. If this collection makes any guarantees about the order of the elements returned by its iterators, the method must return the elements in the same order. The returned array will be "safe" because the collection does not retain a reference to it. (In other words, this method must be assigned a new array, even if the collection is supported by an array). Therefore, the caller is free to modify the returned array. This method acts as a bridge between array-based and collection-based APIs.ResultsAn array that contains all the elements in this collection
    • Clear
void Clear ()

Removes all elements from this collection (optional action). When this method returns, the collection is empty. exception UnsupportedOperationException -If this collection does not support clear operations

    • Add
Boolean Add (E E)
Ensures that this collection contains the specified element (optional action). If this collection is changed because of a call, the returntrue。 (If this collection does not allow duplicates, and the specified element is already included, returnsfalse. A collection that supports this operation may limit the elements that might be added to this collection. In particular, some collections refuse to addNull speciesElements, and others will impose type restrictions on the elements that can be added. The collection class should clearly state in its documentation the restrictions on which elements might be added.

If a collection refuses to add a particular element, it must throw an exception (instead of returning false ) except that it already contains the element. This preserves a collection of invariants that always contain the specified element after this call returns.

Parameters e -to ensure that the elements that exist in this collection
   Results true If this collection is changed because of a call
Abnormal
-If the
UnsupportedOperationException add operation is not supported by this collection
ClassCastException -If the class of the specified element prevents it from being added to this collection
NullPointerException -If the specified element is empty and the collection does not allow empty elements
IllegalArgumentException -If some properties of an element prevent it from being added to this collection
IllegalStateException -If the element cannot be added at this time due to the insertion limit

Java learning about the basic interface--collection interface for a collection framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.