Home > 类库下载 > java类库 > body text

JAVA programming ideas---11.5 List, 11.6 Iterator

高洛峰
Release: 2016-10-17 09:12:52
Original
1545 people have browsed it

The hierarchy diagram (source and network) of the Collection class is as follows:

JAVA programming ideas---11.5 List, 11.6 Iterator

Interface: Iterator

public interface Iterable<T>
Copy after login

Iterableinterface as a super interface, there is only one iterator() with return type Iterable Method, implementing this interface allows an object to be the target of a "foreach" statement.


Interface: Collection

public interface Collection<E> extends Iterable<E>
Copy after login

The root interface in the Collection hierarchy. Collection represents a set of objects, which are also called elements of a collection. Some collections allow duplicate elements (such as List, Queue), while others do not (such as Set). Some collections 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 implementations of more specific sub-interfaces such as Set and List. This interface is typically used to pass collections and operate on them wherever maximum generality is required.

Interface: List

public interface List<E> extends Collection<E>
Copy after login

Ordered collection (also called sequence). The List interface adds a large number of methods based on the Collection interface, allowing precise control of the insertion or removal position of each element in the list. Users can access elements based on their integer index (position in the list) and search for elements in the list.

The List interface adds some other conventions to the contracts of iterator, add, remove, equals and hashCode methods, beyond those specified in the Collection interface.

List interface provides positioning (indexing) access methods to list elements. Lists (like Java arrays) are 0-based. Note that these operations may be performed in a time proportional to the index value of some implementations (such as the LinkedList class). Therefore, if the specific implementation is not known, iterating over the elements of the list (Iterator, or foreach loop) is usually better than traversing the list with index (for loop).

The List interface provides a special iterator called ListIterator. In addition to allowing the normal operations provided by the Iterator interface, this iterator also allows element insertion and replacement, as well as bidirectional access. A method is also provided to obtain a list iterator starting at a specified position in the list.

Abstract class: AbstractCollection

public abstract class AbstractCollection<E> extends Object implements Collection<E>
Copy after login

This class provides the backbone implementation of the Collection interface. Implementation classes that implement the List, Set, and Queue interfaces of the Collection interface can inherit this abstract class to minimize the need to implement this The work required for the interface.

This interface implements other interfaces in Collection except size and iterator, and restricts the add method.

To implement an immutable collection, programmers only need to extend this class and provide implementations of iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.)

To implement a modifiable collection, programmers must additionally rewrite the add method of this class (in addition to extending this class and providing implementations of iterator and size methods) ( Otherwise, UnsupportedOperationException will be thrown), and the iterator returned by the iterator method must also implement its remove method.

Abstract Class: AbstractList

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
Copy after login

This class provides a backbone implementation of the List interface to minimize the work required to implement this interface supported by "random access" data stores such as arrays ArrayList (that is, implement The List interface extends methods outside the Collection interface). For methods that implement the Collection interface, this class is implemented by inheriting the AbstractCollection abstract class. The unimplemented methods of this class include get(int index) and size(), and the set(int index, E element), add(int index, E element), remove(int index), add(E element) methods are restrictions.

For continuous access data (such as LinkedList), AbstractSequentialList should be used first instead of this class.

To implement an unmodifiable list, programmers only need to extend this class and provide implementations of the get(int) and size() methods.

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

Class: ArrayList

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Copy after login

ArrayList is a variable-sized array-based implementation of the List interface. Its bottom layer uses an array implementation, so it has good random access capabilities, but it is not efficient for operations such as inserting and deleting at specified locations.

Each ArrayList instance has a capacity. The capacity refers to the size of the array used to store list elements. When the ArrayList capacity size is not specified, the default capacity of the created instance is 10. As elements are added to the ArrayList, its capacity automatically grows.

The iterators returned by the iterator and listIterator methods of this class are fail-fast: after the iterator is created, the list is not modified in any way at any time unless the list is structurally modified through the iterator's own remove or add methods. When modified, the iterator will throw ConcurrentModificationException. Therefore, in the face of concurrent modifications, the iterator will quickly fail completely, rather than risk arbitrary non-deterministic behavior at some unspecified time in the future.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template