Container classes can greatly improve programming efficiency and programming capabilities. In java2, all containers have been redesigned by Joshua Bloch of SUN Company, enriching the functions of the container class library.
The purpose of the Java2 container class library is to "save objects", which is divided into two categories:
Collection----a set of independent elements, usually these elements obey certain rules. List must maintain a specific order of elements, while Set cannot have duplicate elements.
Map----a set of "key-value pair" objects, that is, its elements are paired objects. The most typical application is the data dictionary, and there are other extensive applications. In addition, Map can return a Set composed of all its keys and a Collection composed of all its values, or a Set composed of its key-value pairs, and it can also extend the multi-dimensional Map like an array, as long as each "value" of the key-value pair in the Map "Just a Map."
1. Iterator
An iterator is a design pattern that is an object that iterates through and selects objects in a sequence without the developer needing to know the underlying structure of the sequence. Iterators are often called "lightweight" objects because they are cheap to create.
The Iterator function in Java is relatively simple and can only move in one direction:
(1) Use the method iterator() to ask the container to return an Iterator. The first time the Iterator's next() method is called, it returns the first element of the sequence.
(2) Use next() to get the next element in the sequence.
(3) Use hasNext() to check if there are still elements in the sequence.
(4) Use remove() to delete the element newly returned by the iterator.
Iterator is the simplest implementation of Java iterator. ListIterator designed for List has more functions. It can traverse List in two directions and can also insert and delete elements from List.
2. Functional methods of List
List(interface): Order is the most important feature of List; it ensures that the specific order of elements is maintained. List adds many methods to Collection, making it possible to insert and remove elements from the List (only recommended for LinkedList). A List can generate a ListIterator, which can be used to traverse the List in two directions and to insert and delete elements from the middle of the List.
ArrayList: List implemented by array. It allows fast random access to elements, but inserts and removes elements from the middle of the List very slowly. ListIterator should only be used to traverse ArrayList from back to front, rather than inserting and deleting elements, because this is much more expensive than LinkedList.
LinkedList: Sequential access is optimized. Inserting and deleting into the middle of the List is not expensive, but random access is relatively slow (ArrayList can be used instead). It has methods addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast(). These methods (not defined in any interface or base class) allow LinkedList to be used as a stack, queue and Use bidirectional queue.
3. Functional methods of Set
Set(interface): Each element stored in Set must be unique, because Set does not save duplicate elements. The Object added to the Set must define the equals() method to ensure the uniqueness of the object. Set and Collection have exactly the same interface. The Set interface does not guarantee that the order of elements is maintained.
HashSet: Set designed for quick search. Objects stored in HashSet must define hashCode().
TreeSet: A Set that maintains order, with a tree structure at the bottom. Use it to extract an ordered sequence from a Set.
LinkedHashSet: It has the query speed of HashSet, and internally uses a linked list to maintain the order of elements (the order of insertion). So when using an iterator to traverse the Set, the results will be displayed in the order in which the elements were inserted.
HashSet uses hash function to sort elements, which is specially designed for fast query; TreeSet uses red-black tree data structure to sort elements; LinkedHashSet uses hash internally to speed up query, and uses linked lists to maintain elements. Order so that it appears that the elements are stored in insertion order. It should be noted that when generating your own class, Set needs to maintain the storage order of elements, so it must implement the Comparable interface and define the compareTo() method.
The above is the analysis of two special Java container classes, List and Set. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!