Java Collection Learning (i)

Source: Internet
Author: User
Tags int size

What is a collection? This is defined and described in the Java Website API documentation:

public interface collection<e>extends iterable<e>

A collection represents a set of objects, each of which is called an element. A collection can consist of repeating elements, or it can be different elements. It can be either unordered or orderly. The JDK does not provide its immediate implementation, but rather provides a more specific sub-interface such as Set,list implementations. The collection interface is usually mentioned when a collection is passed and manipulated with the highest level of abstraction required.

Bag and Multiset (unordered and can contain duplicate elements) related interfaces need to implement the collection interface directly.

All common Collection interface implementation classes (typically implemented on collection Subinterfaces) should contain two standard constructors: one is a non-entry constructor to create an empty collection, and the other is a single-entry parameter with the same type collection, which is used to create a collection that contains the same elements as the entry parameter. The second constructor is often used to copy a collection, that is, to create an equivalent set.

The collection interface also contains some "harmful" methods, that is, the method changes the collection implementation to operations not supported by the current collection, and throws a Unsupportedoperationexception exception. For example, you can call the AddAll method on a non-modifiable collection, but throws an exception when the added collection is empty.

Some collection implementations have stringent requirements for their inclusion elements, such as some implementations that prohibit non-empty elements, and some that have stringent requirements for element types. Attempting to add an illegal element throws a unchecked exception, such as Common NullPointerException and ClassCastException. An attempt to query for an illegal element exists usually throws an exception or returns false directly. More generally, inserting unqualified elements into the collection throws an exception or returns success for some implementations.

The synchronization strategy for different sets depends on the individual collection itself. In the absence of a strong guarantee, invoking a method in a collection that is being changed by another thread can result in an unknown error. Method invocations typically include calling directly, passing a collection to other methods to implement an indirect call, or checking a collection with an iterator that already exists.

The definition of a method in many of the collection framework interfaces relies on the equal method. For example, the usage specification of method contains (Object O) says "True if and only if the collection contains at least one element e that satisfies the condition (o==null? E==null:o.equals (E))." However, this description cannot be interpreted as a call to O.equals (e) when calling collection.contains with non-null entry O. Interface implementations can be freely optimized when equal calls can be avoided when called. For example, you can first compare the hash code of two elements (Object.hashcode () to ensure that different objects produce distinct hash codes). Generalization, as long as the author thinks appropriate, the implementation of the set framework interface can freely utilize the advantages of the underlying object method.

The collection interface is just one member of the Java Collection Framework (Java Collections Framework).

Finally, the method details of the interface are:

int size (): Returns the number of elements in the collection;

Boolean isEmpty (): Returns True when the collection contains no elements;

Iterator<e> Iterator (): Returns an iterator that points to the collection element;

Object[] ToArray (): Returns an array containing the elements of the collection;

Boolean Add (E): Ensure that the collection contains the specified element, which returns True when the collection has actually changed after the method call, and False when the collection already contains the element and does not allow the inclusion of duplicate elements;

Boolean remove (Object o): Removes the specified element from the collection and returns True when the element is removed after the method call;

Boolean containsall (collection<?> C): Returns True when the collection contains all the elements in the specified collection C;

Java Collection Learning (i)

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.