Java Programming Ideas (fourth Edition) Learning notes----11.5 list,11.6 iterators

Source: Internet
Author: User
Tags iterable

The hierarchy diagram for the collection class (source and network) is as follows:

Interface:iterator<t>
 Public Interface Iterable<t>

The Iterable<t> interface acts as a super interface in which there is only one iterator () method that returns a type of iterable<t>, which
implements this interface to allow the object to be the target of a "foreach" statement.


Interface:collection<t>
 Public Interface extends Iterable<e>

The root interface in the Collection hierarchy. Collection represents a set of objects, also known as Collection elements. Some collection allow duplicate elements (such as List,queue), while others do not allow (such as set). Some collection are ordered (such as List,treeset,linkedhashset,treemap,linkedhashmap), while others are unordered (such as Hashset,hashmap). The JDK does not provide any direct implementation of this interface: it provides more specific sub-interfaces (such as Set and List) implementations. This interface is often used to pass collection and operate these collection where it is required to be the most universal.

Interface:list<t>

 Public Interface extends Collection<e>

An ordered collection (also known as a sequence). The list interface adds a number of methods to the collection interface, allowing you to precisely control where each element in the list is inserted or removed. The user can access the element based on the integer index of the element (where it is located in the list) and search for the elements in the list.

List interface in iterator,add,remove,equals , and hashcode The contract for the method is added with some additional conventions that exceed the conventions specified in the Collection interface.

The list interface provides a way to locate (index) access to the elements of a listing. The list (like a Java array) is based on 0. Note that these operations may be performed in a time proportional to the index values of some implementations, such as the LinkedList class. Therefore, if you do not know the implementation, then iterating over the list elements (Iterator, or foreach loops) is generally better than using the index traversal (for loop) list.

The List interface provides a special iterator, called Listiterator, that allows element insertion and substitution, as well as bidirectional access, in addition to the normal operation that the Iterator interface provides. It also provides a way to get a list iterator starting at the specified position in the list.

Abstract class:abastractcollection<t>

 Public Abstract class extends Implements Collection<e>

This class provides the backbone implementation of the Collection interface, which implements the list, Set, and queue interface implementations of the Collection interface that can inherit this abstract class to minimize the work required to implement this interface.

The interface implements the interfaces in the collection other than Size,iterator, and restricts the Add method.

To implement a non-modifiable collection, programmers simply extend this class and provide implementations of the iterator and size methods. (The iterator returned by theiterator method must implement Hasnext and next.) )

To implement modifiable collection, programmers who extend this class and provide implementations of the iterator and size methods must also override this class's add method (otherwise, throw Unsupportedoperationexception), the iterator returned by theiterator method must also implement its remove method separately.

Abstract class:abastractlist<t>

 Public Abstract class extends Implements List<e>

This class provides the List backbone implementation of the interface to minimize the work required to implement the interface that the "Random Access" data store (such as array ArrayList) supports (that is, to implement a method that extends the list interface beyond the collection interface). For methods implemented in the collection interface, this class is implemented by inheriting the Abstractcollection abstract class. Methods not implemented by this class include get (int index) and size (), and set (int index, e Element), add (int index,e element), remove (int index), add (E Element) Method is limited.

For contiguous access data (such as linked list LinkedList), it should be used first AbstractSequentialList instead of this class.

To implement a non-modifiable list, programmers simply extend this class and provide get(int) and size() implement the method.

To implement a modifiable list, the programmer must override the set(int, E) method differently (otherwise it will be thrown UnsupportedOperationException ). If the list is variable size, the programmer must override add(int, E) and remove(int) method differently.

Class:arraylist<t>

 Public class extends Implements List<e>, Randomaccess, cloneable, Serializable

ArrayList is an array-based implementation of the variable size of the list interface. The bottom of the array is implemented in such a way that it has good random access, but it is not efficient for inserting, deleting and so on at the specified location.

Each ArrayList instance has a capacity. This capacity refers to the size of the array used to store the list elements, without specifying a arraylist capacity for a large hour, and the default capacity of the created instance is 10. As you add elements to the ArrayList, their capacity increases automatically.

The iterator returned by the iterator and listiterator methods of this class is a quick failure : After the iterator is created, except through the remove or add< of the iterator itself The/c10> method modifies the list from the structure, otherwise the iterator is thrown whenever the list is modified in any way at any time ConcurrentModificationException . Therefore, in the face of concurrent modifications, the iterator will soon fail completely, rather than risking arbitrary, indeterminate behavior at some uncertain time in the future.

Java Programming Ideas (fourth Edition) Learning notes----11.5 list,11.6 iterators

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.