Java container learning experience Since I am familiar with C , I learned that Java should focus on the new concepts brought by Java. This article is basically the basic concept of the collection framework in the Java standard library, no example. The purpose of writing this article is that I have forgotten these things for a long time. This article can be quickly recalled. 1. The entire Java container class is the basis of the container interface (such as the interface between Collection, Map), not a class. The biggest advantage of using the interface is to separate the implementation of the container with the interface of the container, which means that you can use the same method to access the container without careful container. What kind of data structure is implemented. Similarly, the Iterator interface also enables users to access different container classes using the same method. These are the foundation of the general algorithm. 1.1 Collection Interface Collection Interface There is a basic method: Boolean Add (Object Obj): If the collection does have changed, return true; otherwise return falseiterator iterator (): Returns an object that implements the Iterator interface, There are int size (), Boolean ISempty (), Boolean Contains (Object Object Object Object), Void Clear (), etc. Many useful methods. 1.2 MAP interface MAP is used to store keyword / value pairs. There is the following basic method: Object Get (Object Key) Object Put (Object Key, Object Balue) Set KeySet () SET ENTRYSET () In addition, there are other useful methods. It should be noted that it seems to be a collection of key values, but actually not this. However, on the other hand, if a part of MAP is considered, it is sometimes very convenient. In other words, you can create a collection of collections to express the map of MAP. In summary, a MAP can return to a set of sets of key values, a set consisting of its value or a set of a set of key values. 1.3 Iterator interface Iterator interface has three basic methods: Object next (): Returns the reference to the iterator just crossed element (): Determine if there is also an elements available in the container void Remove (): Delete iterators Just crossed elements: The iterator in Java is very important to the iterators in STL. In STL, iterator is similar to an index of an array, using this iterator to view elements stored in this position (similar to accessing C [i] by array index I). The iterator in Java is not running like this. Viewing the change in the change in the location. Each time you pass () accesses an element, the iterator's location will automatically take a step forward. This problem can be understood: The location points pointed to by the iterator in Java is not an element, but between the elements. In this way, each time you call next (), the iterator passes the next element, and returns the reference to the element it just crossed. According to the above description, it is easy to get the following code is wrong: it.remove (); it.remove (); and the following code is correct: it.remove (); it.next (); IT. Remove (); typical application of iterator it = c.Iterator (); while (it.hasnext ()) {Object obj = it.next (); // do something with obj} 1.4 sub-interface 1.4.1 list Interface List is discrete from the Collection interface because of the characteristics of List - an orderly collection.
The ordered order here is not a sequence of sorted in size, but a collection of sequences that can be accessed in order. This feature of List, which adds a method of operating through an index than the Collection interface. For example, an index value can be added in the parameter table of add, remove, get, set, etc. to operate the elements at the index position. 1.4.2 SET interface set and list, it is disordered; so, the SET object 1.4.3 Listiterator interface is not allowed by any index, add some of the iterator interface. Method (for example, add (), etc.). In addition, since List is a two-way table, Object Previous () and Boolean Hasprevious () methods are also added, and the usage is the same as next () and Hasnext (). 1.4.4 SortedMAP interface contains the following basic method: Comparator Comparator () Object firstkey () 2. Abstract container class 2.3 Abstract container class includes AbstractCollection, AbstractStList, AbstractSet, etc. 2.2 Why have an abstract combination class? For example, many useful methods are defined in the Collection interface. If each class that implements the Collection interface, it will be very troublesome. In order to make the implementation of classes that implement the Collection interface, the AbstractCollection class makes some basic methods (such as add () and item ()) become abstract methods, and other methods of these basic methods (such as addall (), etc. The specific implementation. 3. Specific containers 3.1 ArrayList and LinkedList are classes that implement the List interface, is an ordered set. List interface supports access to elements through an index, for this, ArrayList does not have any problems; but there is a big problem for LinkedList, and the list should not support random storage, but as a List, the linked list also provides Random access is supported, but the efficiency is very low. Each time the index is used to conduct a traversal. I think it should not let the list support to join the random access; Java implements I want to be because of the system of the entire collection framework, so that the linked list can be used in the same way. In summary, it is best not to use random access for LinkedList, and use iterators. 3.2 Treeset3.2.1 Treeset is an implementation of sortedset. Depending on the knowledge of the data structure, it is known that the efficiency of the tree is very high, and there is a class such as Treeset in the Java standard library, and Treeset should be used to improve the efficiency of the program. 3.2.2 Note that: Treeset is a sequence set, which is sorted by CompareTo or Comparator. Any element having the same value (whether it is equals ()), in the Treeset as the same element, so that there is no repetition. In this way, even if different objects cannot be added to the collection, this is sometimes inconvenient. When writing a * algorithm, different status sometimes corresponds to the same inspiration function value, then these different states cannot be added to the Treeset.
3.3 Hashset3.3.1 Hashset is a very efficient data structure. Unlike TREESET, Hashset is the equals () method of the comparison object to distinguish between different objects. This will only be added to the collection only if the object is not repeated. 3.3.2 It is to be noted that it is that hashset efficiency is very high, but the HashCode function of the object is not determined. The HashCode function of the general default object is obtained according to the memory address of the object. A good Hashcode function is the key to successful use of Hashset. 4. View 4.1 What is a view? Use the keyset () method for the image class, as if this method has established a new collection and all keywords affecting are in this collection. The actual situation is not the case, any operation of this collection will be reflected on the original image object. In fact, KeySet () returns an object that implements the SET interface, the operation of the object is the operation of the image. Such a collection is a view. 4.2 Application of View 4.2.1 Change existing containers to thread safely: Using Collections.SynchronizedCollection (Collection C) Method, the interpretation of this method in the SDK document is "Returns a synchronized (thread-safe) Collection Backed By "The Specified Collection". 4.2.2 Change the existing container to a read-only container: Using the Collections.unmodifiableCollection (Collection C) method, the interpretation of this method in the SDK document is "Returns An Unmodifiable View of The Specified Collection.". 4.2.3 Sub-range 4.2.4 ASLIST () method in the Arrays class 5. A general algorithm that is generally used by common algorithm is to write general algorithms. You can use the static general methods in Collectes, you can also write your own general methods. (The content of the specific algorithm is slightly here) summary: Millions Remember this sentence - there is no best container (data structure), to choose different containers according to different issues to achieve functional requirements and Optimism of efficiency. Author Blog:
http://blog.9cbs.net/jjjer/