Java containers of learning experience
Java containers of the learning experience because I am more familiar with C + +, Java learning Java should be the focus of the new concept. This paper is basically Java standards set in the framework of the basic concepts, not examples. The purpose of this paper is to write for a long time after I forgot If these things can be quickly recalled this article together.
1. Interface of the Java container is the foundation of containers interface (such as Collection, such as Map Interface), and not the class. Use interface will be the biggest advantage of the realization of containers and container interface separately, which means you can use the same method to containers and containers is not concerned about what kind of data structure to achieve. Similarly, the Iterator interface also enables users to use the same methods to different containers category. These generic algorithm is the basis.
1.1 Collection Interface
Collection interface has the following basic methods:
Boolean add (Object obj): If add an object, the changes have indeed occurred in pools, then returns true, otherwise returns false
Iterator iterator (): return to a realization of the target of the Iterator interface In addition,
Int size (), boolean isEmpty (), boolean contains (Object obj), void clear (), and many other useful ways
1.2 Map Interface
Map used for the storage keyword / value right. There are basic ways:
Object get (Object key)
Object put (Object key, Object balue)
Set keySet ()
Set entrySet ()
In addition, there are other useful ways.
It is noteworthy that the surface it seems that the keys to constitute a set, but in fact is not the case. But on the other hand if the Map will be seen as a part of assembly, or sometimes it is very convenient. In other words you can use it to create a pool to express that part of the Map. To sum up, one can return to the Map of things including its keys consisting of a Set, which will constitute the value of a set of keys, or by its constituting a Set.
1.3 Iterator interface
Iterator interface has the following three basic methods:
Object next (): Back iterators just across the application of the elements
Boolean hasNext (): determine whether there are containers for the visit to the elements
Void remove (): Delete iterators just crossed the elements NOTE: Java in iterators and STL in the concept of iterators in a very important distinction. In STL, iterators similar to the array index, the use of such iterators can view stored in the position of the elements (similar to the array index i through to visit the same c [i]). Java is not the iteration of this operation. Show closely with the changes in the location of the combine. Each through next () an element of the visit, iterators position will be automatically move forward step.
This problem can understand: Java in the iterators are not at the location of elements, but elements. In this way, each invocation next (), the next iteration of an element will be crossed, but it has just crossed the return of the elements that are cited.
According to the above note, it is easy to draw the following code is wrong:
It.remove ();
It.remove ();
The following code is correct:
It.remove ();
It.next ();
It.remove ();
Typical applications iterators
Iterator it c.iterator = ();
While (it.hasNext ())
(
Object obj = it.next ();
/ / Do something with obj
)
- Interface 1.4
1.4.1 List Interface
List from the Collection interface separation from the List because of the characteristics - orderly collection. Here, in an orderly manner that is not in accordance with the sequence of good size Pai (Sorted), but that can be set to determine the order of the sequence visit. List the features of this view, it has increased the Collection interface to operate through indexing method. For example, add, remove, get, set the parameters of the methods in the table can be added to the index value, thus operating in the location of the index elements.
1.4.2 Set interface
Set List and the different elements inside it is disorderly Therefore, the index can not be any way to operate Set Object
1.4.3 List ListIterator interface with the iteration, than Iterator interface, there are some methods (such as add (), etc.). In addition, because List is a two-way table, it has also increased the Object previous () and boolean hasPrevious (), usage and next () and hasNext () the same.
1.4.4 SortedMap interface contains the following basic methods:
Comparator comparator ()
Object firstKey ()
Object lastKey ()
2. Abstract category containers
2.1 abstract category includes AbstractCollection containers, AbstractList, AbstractSet etc.
Why do we need with 2.2 abstract category?
For example, in the Collection interface definition many useful ways, if realized Collection interface for each category have their own methods to achieve so much, it would be very troublesome. In order to achieve Collection of the realization of the interface more easily, AbstractCollection category for some basic methods (such as add () and iterator ()) into the abstract approach, and the use of these basic methods of other methods (for example, addAll () , etc.), specific has been achieved.
3. Specific containers
3.1 ArrayList and LinkedList
List is achieved interface, there are the ordered set. List interface support indexing methods to access elements of this point, ArrayList do not have any problems, but the LinkedList is a big problem, linked list itself should not support random storage, but as a realization of the List, also provided Listless Random visits to the support, but inefficient. Each method are indexed by an ergodic. I think, in fact we should not let Listless support random access, and Java to achieve this is because I would like to set the framework of the whole system, making linked list and the array can be used the same method to use. To sum up, the LinkedList best not to use random access, and use of iterators.
3.2 TreeSet
TreeSet SortedSet 3.2.1 is a realization. According to the data structure knowledge can know that the tree is highly efficient, and in the Java standard library TreeSet this category, should make full use of TreeSet future to improve the effectiveness of the procedure.
3.2.2 needs attention is: TreeSet as ordered set, it adopted compareTo or Comparator will bring together elements to sort. Any comparison with the same value of the elements (whether or not they are equals ()), both in the TreeSet as the same elements, and thus can not be duplicated. Thus, even if the target is different not join the pool, this is sometimes inconvenient. I am in the preparation of A * algorithm, corresponding to different states sometimes with a heuristic function value, then these different to the state will not be able to join in the TreeSet.
3.3 HashSet
3.3.1 HashSet is very efficient data structure, and TreeSet different, the object is a HashSet equals () method to distinguish between different objects. This only real difference can not be the object of repeated join the pool.
3.3.2 needs attention is: HashSet very efficient, but not object hashCode function identified. General default object hashCode function is based on object memory address that. HashSet good hashCode function is the key to successful use.
4. View
4.1 What is a view?
On the mapping of the use of keySet () method, the method as if the establishment of a new pool, and of all the keywords are entered this set. The actual situation is not the case, this set of any operation will be reflected in the original mapping object.
In fact, keySet () is a realization Set interface object, the object of the operation is to mapping operation. This collection of a view.
4.2 Application of view
4.2.1 existing thread-safe containers into containers: Use Collections.synchronizedCollection (Collection c), in the SDK documentation of the method explained that "Returns a synchronized (thread-safe) collection backed by the specified collection. "
4.2.2 will be available on CD-containers into containers: Use Collections.unmodifiableCollection (Collection c), in the SDK documentation of the method explained that "Returns an unmodifiable view of the specified collection.."
4.2.3 scope of
4.2.4 Arrays in category asList () method
5. Generic algorithm common set of the interfaces is a major advantage can be the development of generic algorithm. Collections can be used in the static general method can also prepare their own generic methodologies.
(Specific algorithm in the content of this omitted)
Summary: 10000000 remember this phrase - not the best containers (data structures), according to the different issues in different containers, in order to meet functional requirements and the optimal efficiency.






