Java.util Collection Framework Collection

Source: Internet
Author: User

The collection framework for Java provides a standard way for programs to work with groups of objects. A series of standard generic interfaces are designed: the ⑴collection () interface expands the Iterable interface at the top of the collection hierarchy, so all the collections implement the Collection interface and provide a iterator () method to return an iterator. Add the object with the Add () method, remove the element, clear () Delete all elements of the collection (Size=0), contains () method to see if the collection contains an object, and the ToArray () method returns an array of collection elements. The Equals () method compares two sets for equality, the size () method returns the number of elements in the collection, IsEmpty () determines whether the collection is empty, hashcode () returns the hash code of the calling collection, and iterator () returns the iterator that invokes the collection. The  ⑵list () interface, which extends the collection interface, stores the elements of a sequence (the list is based on the 0 index) and can contain duplicate elements, but cannot have null values. An object that obtains a specific location calls the Get () method, assigns a value to a particular location element with the set () method, and uses the indexof () or LastIndexOf () method to obtain the location of the first instance or the last instance of the object, respectively, and the Sublist () method takes a child list. Listiterator () returns an iterator. The  ⑶set interface extends the collection interface, which does not allow the same elements (including unique null values) to exist. The SortedSet interface expands the set interface and declares that it is an ascending collection. The first () or last () method obtains a single or final object, subset () obtains a subset, and the HeadSet () and Tailset () methods obtain a subset from scratch or until the end of the tail, respectively.  ⑷queue (queue) interface, expands the collection interface, and declares a queue behavior (FIFO, first-in, in-advance list). The offer () method inserts an element at the end of the queue. The element () and peek () methods obtain but not remove the first element of the queue, and if the queue is empty, element () throws a Nosuchelementexception exception, and peek () returns NULL. The poll () and remove () methods get and remove the first element of a queue, and if the queue is empty, poll () returns Null,remove () throws an exception.  ⑸comparator (Comparator) interface, defines two methods: the Compare () and the Equals () method, which, by overwriting the Compare () method, can change the ordering of objectsWay.  ⑹iterator (iterator) interface, Listiterator (list iterator, bidirectional traversal) interface, in general, The following steps should be followed when iterating through the collection using iterators: A Iterator () from the collection gets a pointer to the iterator at the beginning of the collection: iterator itr= Collectionobject.iterator (); two sets a loop that determines if there is a next element: such as while (Itr.hasnext ()) {. Three takes the next element out of the loop: such as Object Element=itr.next ();  ⑺randomaccess (random access) interface. Implemented by the ArrayList class and the remaining vector classes.  ⑻map (map) interface, Map.entry (nested class for map, inner Class) interface, SortedMap (default by key key in ascending order) interface. A map is an object that maps keys to values, a map cannot contain duplicate keys, and each key can only map one value at a maximum. The map interface provides three collection views: A keyset, a value collection, or a key-value mapping relationship set. Mappings are not collections because they do not implement the collection interface, but you can obtain a collection view of the key-value mappings by using the EntrySet () method, or use the keyset () method to get a collection view of the keys, using the values () method to get a collection view of the values. The put () method puts the value in the map, and get () gets the value associated with the key K. The SortedMap interface adds a headmap (), Tailmap (), SubMap () method to get a sub-map, and a Firstkey (), Lastkey () method to get the key. The Map.entry interface defines the SetValue () method that gets the Getkey (), GetValue () method, and replacement value of the key value. The order of the collection collection is the Set,map order is the TreeMap. Exception: Attempting to add an incompatible object to the collection or mapping will throw classcastexception, if an invalid index is used, throwing indexoutofboundexception;null is not allowed to exist in the collection or mapping if you attempt to use a null object , throws a nullpointexception, throws a unsupportedoperationexception when modifying an immutable set or map, and throws a nosuchelementexception when the next object does not exist. ; Set frame interface schematic (parent class ∈ Subclass): 1, CollectioN∈⑴list,⑵set∈sortedset. 2, Map∈sortedmap.

Java.util Collection Framework collection

Related Article

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.