History of Collection frame work:
Java collections are a legacy frame work which contains classes and interfaces that implement commonly reusable collection of data structures. Java collection framework (JCF) it works as a library.
Collection frame work was designed and developed primarily by Joshua Bloch and was introduced in JDK1.2 version. Bloch reused many ideas of Doug Lea’s collection’s package, which was deprecated as a result, Sun chose not to use the ideas of JGL, because they wanted a compact framework, and consistency with C++ was not one of their goals
Doug Lea later developed a concurrency package comprising new Collection-related classes. An updated version of these concurrency utilities was included in JDK5.0 as of JSR 166.
Collection implementations in pre-JDK 1.2 versions of the Java platform included few data structure classes, but did not contain a collections framework. The standard methods for grouping Java objects were via the array, the Vector and the Hashtable classes, which unfortunately were not easy to extend, and did not implement a standard member interface.
To address the need for reusable collection data structures (A data structure is a data organization and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data)several independent frameworks were developed, the most used being Doug Lea’s Collections package, and ObjectSpace Generic Collection Library (JGL),whose main goal was consistency with the C++ Standard Template Librarry (STL).
Collection Frame work:
A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language — or just about any other programming language — you are already familiar with collections.
Architecture of Collection Framework:
Collection defines the basic parts of all collections. All collections in Java are derived from the java.util.Collection interface.
The basic methods of interface is add() and remove() for adding and remove an element from a collection.
Also required is the toArray() method, which converts the collection into a simple array of all the elements in the collection.
Finally, the contains() method checks if a specified element is in the collection. The Collection interface is a sub interface of java.lang.Iterable, so any Collection may be the target of a for-each statement
Additionally, Collection is a generic. Any collection can be written to store any class. For example, Collection<String> can hold strings, and the elements from the collection can be used as strings without any casting required. Note that the angled brackets < > can hold a type argument that specifies which type the collection holds.
Types of Collections:
We have three generic types of collections.
- Ordered List
- Dictionaries or maps
Ordered lists allows the programmer to insert items in a certain order and retrieve those items in the same order.
An example is a waiting list. Two interfaces are included in the Ordered Lists which are the List Interface and the Queue Interface.
Dictionaries/Maps store references to objects with a lookup key to access the object’s values.
One example of a key is an identification card. The Map Interface is included in the Dictionaries/Maps.
Sets are unordered collections that can be iterated and where similar objects are not allowed. The Interface Set is included
- Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
- Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
- Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.
Benefits of Collection framework:
- The collection frame work achieved high-performance.
- Reduces programming effort – By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level “plumbing” required to make it work. By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
- Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you’re freed from the drudgery of writing your own data structures, you’ll have more time to devote to improving programs’ quality and performance
- Allows interoperability among unrelated APIs: The collection interfaces are the vernacular by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, our APIs will interoperate seamlessly, even though they were written independently.
- Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection interfaces, the problem went away.
- Reduces effort to design new APIs: This is the flip side of the previous advantage. Designers and implementers don’t have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.
- Fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces