Collection introduction. Lets Start with
Java did not have Collection framework in earlier versions. Instead it defined classes and a interface to store objects.
These older classes are known are legacy classes
Legacy classes- Dictionary, HashTable, Properties, Stack, Vector
Legacy interface- Enumeration
What is Collection?
Collection is a set of fourteen collection interfaces and classes(implementations of these interfaces, and algorithms to manipulate them).
All the collection classes and interface are contained in java.util package.
You can use these classes according to the need.
Collections : A utility class that is a member of the Java Collections Framework.
Collection : The root interface of Java Collections Framework/hierarchy.
This means that every single class implementing Collection in any form is part of the Java Collections Framework.
Q: One question arise in mind that may confuse, It is collection framework or collections framework?
A: Framework name is collections and the interface name is Collection. Do not get confused.
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, have been 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 primitives and reference objects. Not, strictly speaking, a part of the Collections Framework, this functionality was added to the Java platform at the same time and relies on some of the same infrastructure.
Q: Map is a collection or not or Why does Map not extend Collection interface?
A: Collection assume elements of one value. Map assumes entries of key/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.
Collection.remove(Object) – removes an element.
Map.remove(Object) – removes by key, not by entry.
You could model a Map as a collection of entries, which is what Map.entrySet() does.
There are some methods in common; size(), isEmpty(), clear(), putAll/addAll() but these are unlikely to have much value as a stand alone interface. (Again Map.entrySet() can be used instead)