Implementation of the Java API (top)

Source: Internet
Author: User

Realize

An implementation is the actual data object used to store the set of objects, which implements the core set of objects described in the previous chapters. The following sections describe three implementations:

Universal implementation

A common implementation is a public class that provides the primary implementation of the core object set interface.

Wrapper implementation

The wrapper implementation provides additional functionality along with other implementations, usually the common implementation.

Facilitate the implementation

A convenience implementation is a small implementation that can often be obtained through a static method (the static factory methods) that facilitates and efficiently replaces a common implementation for a particular set of objects (like singleton sets).

In addition, depending on the abstract implementations of the JDK, you can also build your own implementation. This is described in a separate course because it belongs to the advanced curriculum. It is not particularly difficult, but in relative terms, few people need it.

Universal implementation

The following table summarizes the common implementations. The table highlights their normal naming styles: the names are in the form, where the core object set interface is implemented by the class, and represents the data structure at the bottom of the implementation.

    

implementation
hash Table resizable d> balanced tree linked List
interface set hashset   treeset  
  arraylist   linkedlist
maP hashmap   treemap  

JDK 1.2 provides two implementations of each interface (collection is an exception, it has no direct implementation, but it can be treated as the lowest common denominator of other object set interfaces). In each of these interfaces, one of the implementations is obviously the master implementation: the one to be used, the other things being the same. The main implementation is HashSet, ArrayList and HashMap. Note the SortedSet and SORTEDMAP interfaces are not listed in the previous table. Each of them has a single implementation, and these implementations (TreeSet and TREEMAP) are listed in the set and Map columns.

These implementations have not only consistent names, but also consistent behavior. They all implement the option operations (optional operations) that are contained in their interfaces, allowing null elements, keys, and values. Each of these implementations is not synchronized. They all have fast-fail iterations that can detect illegal concurrent changes in an iterative process and quickly and completely fail, rather than risk any indeterminate behavior at an unpredictable time in the future. All implementations are serializable (Serializable) and support the public clone method.

The fact that the new implementation is out of sync represents an interruption from the past: vectors and Hashtable are synchronized in the previous version of JDK 1.2. The new approach is taken because the set of objects is often used in a way that is not beneficial in a synchronization. Such uses include single-threaded use, read-only use, and use as part of a larger data object (it has its own synchronization). In general, API design practices do not allow users to spend money for unnecessary performance. Furthermore, unnecessary synchronization may lead to deadlocks in certain situations.

If you need a synchronized set of objects, the synchronization wrappers (the synchronization wrapper) (described in the next section) allows any set of objects to be converted to a set of synchronized objects. Therefore, for the new object set implementation, synchronization is optional when it is mandatory for the old set of objects. As a rule of thumb, you should consider interfaces, not implementations. This is why there are no programming instances in this section. For the most part, the choice of implementation only affects performance. The preferred style (as mentioned in the interface course) is to select an implementation when creating a set of objects and immediately assign a new set of objects to a variable of the corresponding interface type (or pass the object set to a method of the parameter for this interface type). In this way, the program will not rely on any additional methods in a given implementation, and give programmers and maintainers a quick change in the freedom of implementation (if the performance is allowed to do so).

The general implementation is briefly discussed below. The performance description of the implementation uses words such as constant, log, linear, n log (n), and quadratic. These words refer to the progressive upper bound (asymptotic upper bound) of time complexity (complexity) for performing operations. All of this is enough for you to digest, but if you don't understand it, it doesn't matter much. If you are interested, you can read any of the algorithm textbooks, which have an explanation of such content. One thing to keep in mind is that this metric of performance has its limitations. Sometimes a nominally slow implementation may be faster for a set of objects that you actually use in size. If there is doubt, weigh the performance.

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.