Java Collection Framework
Before Java 2, Java provided the ad hoc class. For example: Dictionary, Vector, Stack, and properties These classes are used to store and manipulate groups of objects.
Although these classes are useful, they lack a central, unified theme. For this reason, the way you use the vector class differs greatly from the way you use the Properties class.
The collection framework is designed to meet several goals.
- The framework must be high-performance. The implementation of the basic set (dynamic array, linked list, tree, hash table) must also be efficient.
- The framework allows different types of collections to work in a similar way, with a high degree of interoperability.
- The expansion and adaptation of a set must be simple.
To do this, the entire collection framework is designed around a standard set of interfaces. You can use the standard implementations of these interfaces directly, such as LinkedList, HashSet, and TreeSet, and you can implement your own collections via these interfaces.
A collection framework is a unified schema used to represent and manipulate collections. All the collection frameworks include the following:
- interface: is an abstract data type that represents a collection. The interface allows the collection to manipulate the details of its representatives independently. In object-oriented languages, interfaces usually form a hierarchy.
- implementation (Class): is the specific implementation of the collection interface. Essentially, they are reusable data structures.
- algorithm: is a useful calculation for the execution of methods in an object that implements a collection interface, for example: Search and sort. These algorithms are called polymorphic, because the same approach can have different implementations on similar interfaces.
In addition to the collection, the framework also defines several map interfaces and classes. A key/value pair is stored in the map. Although maps are not collections, they are fully integrated in the collection.
Collection interface
The collection framework defines some interfaces. This section provides an overview of each interface:
Serial Number |
Interface Description |
1 |
Collection interface Allows you to use a set of objects, which is the root interface of the collection hierarchy. |
2 |
List interface An element that inherits from Collection and a list instance stores an ordered set. |
3 |
Set Inherited from Collection, is a collection that does not contain duplicate elements. |
4 |
SortedSet Inherit from set that holds an ordered collection. |
5 |
Map Maps a unique key to a value. |
6 |
Map.entry Describes an element in a map (a key/value pair). is the inner class of a map. |
7 |
SortedMap Inherit from map so that key remains in ascending order. |
8 |
Enumeration This is a traditional interface and a defined method through which you can enumerate (get one at a time) an element in the collection of objects. This traditional interface has been superseded by iterators. |
Collection Class
Java provides a set of standard collection classes that implement the collection interface. Some of these are concrete classes that can be used directly, while others are abstract classes that provide partial implementations of the interface.
The standard collection classes are summarized in the following table:
Serial Number |
class Description |
1 |
Abstractcollection Most of the collection interfaces are implemented. |
2 |
Abstractlist Inherits from Abstractcollection and implements most of the list interfaces. |
3 |
Abstractsequentiallist Inherits from Abstractlist, providing chained access to data elements rather than random access. |
4 |
LinkedList Inherited from Abstractsequentiallist, a linked list was implemented. |
5 |
ArrayList The dynamic array is implemented by inheriting abstractlist. |
6 |
Abstractset Inherits from Abstractcollection and implements most of the set interfaces. |
7 |
HashSet Inherits the Abstractset, and uses a hash table. |
8 |
Linkedhashset A hash table and a link list implementation of a Set interface with predictable iteration order. |
9 |
TreeSet Inherit from Abstractset, ordering elements in the natural order of the elements. |
10 |
Abstractmap Most of the map interfaces are implemented. |
11 |
HashMap Inherits the HashMap, and uses a hash table. |
12 |
TreeMap Inherits the Abstractmap, and uses a tree. |
13 |
Weakhashmap Inherits the Abstractmap class, using the weak key hash table. |
14 |
Linkedhashmap Inherit from HashMap, ordering elements in the natural order of the elements. |
15 |
Identityhashmap Inherits the Abstractmap class, which uses reference equality when comparing documents. |
The classes defined in the Java.util package have been discussed in the previous tutorial as follows:
Serial Number |
class Description |
1 |
Vector The vector class implements a dynamic array. and ArrayList and similar, but the two are different. |
2 |
Stack A stack is a subclass of a vector that implements a standard LIFO stack. |
3 |
Dictionary The Dictionary class is an abstract class used to store key/value pairs, similar in function to the map class. |
4 |
Hashtable Hashtable is part of the original Java.util and is a dictionary concrete implementation. |
5 |
Properties Properties inherit from Hashtable. Represents a persisted set of properties. Each key and its corresponding value in the attribute list is a string. |
6 |
BitSet A Bitset class creates a special type of array to hold the bit values. The array size in the bitset is incremented as needed. |
A Bitset class creates a special type of array to hold the bit values. The array size in the bitset is incremented as needed.
Set algorithm
The collection framework defines several algorithms that can be used for collections and mappings. These algorithms are defined as static methods of the collection class.
Some methods can throw classcastexception exceptions when trying to compare incompatible types. Throws an unsupportedoperationexception exception when attempting to modify a non-modifiable collection.
A collection defines three static variables: Empty_set empty_list,empty_map. None of these variables can be changed.
Serial Number |
Algorithm Description |
1 |
Collection algorithms Here is the implementation of all the algorithms in a list. |
How to use iterators
Typically, you would want to iterate through the elements in a collection. For example, displays each element in the collection.
The simplest way to do this is to use an iterator, which is an object that implements the iterator interface or the Listiterator interface.
An iterator that enables you to iterate through the elements of the collection or delete them. Listiterator inherits the iterator to allow bidirectional traversal of the list and modify the elements.
All the methods provided by the iterator and Listiterator interfaces are listed here by example. How to use the comparator
TreeSet and TreeMap store elements in sorted order. However, this is done by the comparator to precisely define what sort order to follow.
This interface allows us to sort a collection in different ways.
Serial Number |
Comparator Method Description |
1 |
Using Java Comparator All the methods provided by the comparator interface are listed here by example |
Summarize
The Java Collection Framework provides programmers with pre-packaged data structures and algorithms to manipulate them.
A collection is an object that can hold references to other objects. The collection interface declares the actions that can be performed on each type of collection.
The classes and interfaces of the collection framework are in the Java.util package.
Java Collection Framework