Interfaces section, is to choose an implementation when a Collection is created and to immediately assign the new collection to a variable of the corresponding interface type (or to pass the collection to a method expecting an argument of the interface type). For the most part, the choice of implementation affects only performance. That is why there are no programming examples in this section. These implementations offer much higher concurrency than mere synchronized implementations.Īs a rule, you should be thinking about the interfaces, not the implementations. Moreover, the package provides concurrent implementations of the BlockingQueue interface, which extends Queue, and of the ConcurrentMap interface, which extends Map. Thus, synchronization is optional for general-purpose implementations, whereas it is mandatory for legacy implementations. Wrapper Implementations section, allow any collection to be transformed into a synchronized collection. If you need thread-safe collections, the synchronization wrappers, described in the Furthermore, unnecessary synchronization can result in deadlock under certain circumstances. In general, it is good API design practice not to make users pay for a feature they don't use. Such uses include single-threaded use, read-only use, and use as part of a larger data object that does its own synchronization. The present approach was taken because collections are frequently used when the synchronization is of no benefit. The fact that these implementations are unsynchronized represents a break with the past: The legacy collections Vector and Hashtable are synchronized. All are Serializable and all support a public clone method. All have fail-fast iterators, which detect illegal concurrent modification during iteration and fail quickly and cleanly rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. All permit null elements, keys, and values. These two implementations provide very different semantics: LinkedList provides FIFO semantics, while PriorityQueue orders its elements according to their values.Įach of the general-purpose implementations provides all optional operations contained in its interface. PriorityQueue, which is omitted from the table. LinkedList, which is also a List implementation, and There are two general-purpose Queue implementations TreeMap) and is listed in the Set and the Map rows. Each of those interfaces has one implementation SortedMap interfaces do not have rows in the table. HashMap is clearly the one to use for most applications, all other things being equal. General-purpose Implementations InterfacesĪs you can see from the table, the Java Collections Framework provides several general-purpose implementations of the The general-purpose implementations are summarized in theįollowing table. An advanced topic, it's not particularly difficult, but relatively few people will need to do it. Abstract implementations are skeletal implementations that facilitate the construction of custom implementations described later in theĬustom Collection Implementations section.
Convenience implementations are mini-implementations, typically made available via static factory methods, that provide convenient, efficient alternatives to general-purpose implementations for special collections (for example, singleton sets).Wrapper implementations are used in combination with other types of implementations, often the general-purpose ones, to provide added or restricted functionality.These implementations are part of the package. Concurrent implementations are designed to support high concurrency, typically at the expense of single-threaded performance.Special-purpose implementations are designed for use in special situations and display nonstandard performance characteristics, usage restrictions, or behavior.They are summarized in the table titled General-purpose-implementations. General-purpose implementations are the most commonly used implementations, designed for everyday use.This lesson describes the following kinds of implementations: Implementations are the data objects used to store collections, which implement the interfaces described in