Arrays & Collections
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
Limitations of Array
1. They cannot dynamically shrink and grow.
2. They have limited type safety.
3. Implementing efficient, complex data structures from scratch would be difficult.
Arrays and collection classes/interfaces
The Java platform includes a collections framework. A collection is an object that represents a group of objects (such as the classic Vector class). A collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.
The primary advantages of a collections framework are that it:
* Reduces programming effort by providing data structures and algorithms so you don't have to write them yourself.
* Increases performance by providing high-performance implementations of data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be tuned by switching implementations.
* Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
* Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
* Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs.
* Fosters software reuse by providing a standard interface for collections and algorithms with which to manipulate them.
The collections framework consists of:: -
* Collection interfaces: - Represent different types of collections, such as sets, lists, and maps. These interfaces form the basis of the framework.
* General-purpose implementations: - Primary implementations of the collection interfaces.
* Legacy implementations: - The collection classes from earlier releases, Vector and Hashtable, were retrofitted to implement the collection interfaces.
* Special-purpose implementations: - Implementations designed for use in special situations. These implementations display nonstandard performance characteristics, usage restrictions, or behavior.
* Concurrent implementations: - Implementations designed for highly concurrent use.
* Wrapper implementations: - Add functionality, such as synchronization, to other implementations.
* Convenience implementations: - High-performance "mini-implementations" of the collection interfaces.
* Abstract implementations: - Partial implementations of the collection interfaces to facilitate custom implementations.
* Algorithms: - Static methods that perform useful functions on collections, such as sorting a list.
* Infrastructure: - Interfaces that provide essential support for the collection interfaces.
* Array Utilities: - Utility functions for arrays of primitive types and reference objects. Not, strictly speaking, a part of the collections framework, this feature was added to the Java platform at the same time as the collections framework and relies on some of the same infrastructure.
Collection Interfaces:
The collections framework defines several interfaces. This section provides an overview of each interface:
Sno | Interfaces with Description |
---|---|
1 | The Collection Interface This enables you to work with groups of objects; it is at the top of the collections hierarchy. |
2 | The List Interface This extends Collection and an instance of List stores an ordered collection of elements. |
3 | The Set This extends Collection to handle sets, which must contain unique elements |
4 | The Sorted Set This extends Set to handle sorted sets |
5 | The Map This maps unique keys to values. |
6 | The Map.Entry This describes an element (a key/value pair) in a map. This is an inner class of Map. |
7 | The Sorted Map This extends Map so that the keys are maintained in ascending order. |
8 | The Enumeration This is legacy interface and defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superceded by Iterator. |
Sno | Classes with Description |
---|---|
1 | AbstractCollection Implements most of the Collection interface. |
2 | AbstractList Extends AbstractCollection and implements most of the List interface. |
3 | AbstractSequentialList Extends AbstractList for use by a collection that uses sequential rather than random access of its elements. |
4 | LinkedList Implements a linked list by extending AbstractSequentialList. |
5 | ArrayList Implements a dynamic array by extending AbstractList. |
6 | AbstractSet Extends AbstractCollection and implements most of the Set interface. |
7 | HashSet
Extends AbstractSet for use with a hash table. |
8 | LinkedHashSet
Extends HashSet to allow insertion-order iterations. |
9 | TreeSet
Implements a set stored in a tree. Extends AbstractSet. |
10 | AbstractMap Implements most of the Map interface. |
11 | HashMap
Extends AbstractMap to use a hash table. |
12 | TreeMap
Extends AbstractMap to use a tree. |
13 | WeakHashMap
Extends AbstractMap to use a hash table with weak keys. |
14 | LinkedHashMap
Extends HashMap to allow insertion-order iterations. |
15 | IdentityHashMap
Extends AbstractMap and uses reference equality when comparing documents. |
Interface | Hash Table | Resizable Array | Balanced Tree | Linked List | Hash Table + Linked List |
---|---|---|---|---|---|
Set |
HashSet | TreeSet | LinkedHashSet | ||
List |
ArrayList | LinkedList | |||
Deque |
ArrayDeque | LinkedList | |||
Map |
HashMap | TreeMap | LinkedHashMap |