Résumé
The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them. Collections and arrays are similar in that they both hold references to objects and they can be managed as a group. However, unlike arrays, Collections do not need to be assigned a certain capacity when instantiated. Collections can grow and shrink in size automatically when objects are added or removed. Collections cannot hold primitive data types such as int, long, or double. Instead, Collections can hold wrapper classes such as , , or . Collections are generic and hence invariant, but arrays are covariant. This can be considered an advantage of generic objects such as when compared to arrays, because under circumstances, using the generic instead of an array prevents run time exceptions by instead throwing a compile-time exception to inform the developer to fix the code. For example, if a developer declares an object, and assigns the object to the value returned by a new instance with a certain capacity, no compile-time exception will be thrown. If the developer attempts to add a to this object, the java program will throw an . On the other hand, if the developer instead declared a new instance of a as , the Java compiler will (correctly) throw a compile-time exception to indicate that the code is written with incompatible and incorrect type, thus preventing any potential run-time exceptions.The developer can fix the code by instantianting as an object. If the code is using Java SE7 or later versions, the developer can instatiate as an object by using the diamond operator Collections are generic and hence reified, but arrays are not reified. Collection implementations in pre-JDK 1.2 versions of the Java platform included few data structure classes, but did not contain a collections framework.
À propos de ce résultat
Cette page est générée automatiquement et peut contenir des informations qui ne sont pas correctes, complètes, à jour ou pertinentes par rapport à votre recherche. Il en va de même pour toutes les autres pages de ce site. Veillez à vérifier les informations auprès des sources officielles de l'EPFL.