Lists are implemented in the collections framework via the. ![]() ![]() The base interface for sets is called Set. Sets are unordered collections that can be iterated and contain each element at most once. Lists are finite collections where it can store the same value multiple times. The base interface for dictionaries/maps is called Map. One example of a key is an identification card. The base interfaces for queues are called Queue.ĭictionaries/Maps store references to objects with a lookup key to access the object's values. Queues allow the programmer to insert items in a certain order and retrieve those items in the same order. There are several generic types of Collection: Queues, maps, lists and sets. Note that the angled brackets can hold a type argument that specifies which type the Collection holds. No casting is required when using the String objects from an implementation of Collection. For example, any implementation of Collection contains String objects. (The Iterable interface provides the iterator() method used by for-each statements.) All Collections have an that goes through all of the elements in the Collection.Ĭollection is generic. The Collection interface is a subinterface of, so any Collection may be the target of a for-each statement. Finally, theĬontains(E e) method checks if a specified element exists in the Collection. It also has the toArray() method, which converts the Collection into an array of Objects in the Collection (with return type of Object). The interface has the add(E e) and remove(E e) methods for adding to and removing from a Collection respectively. Collection defines the basic parts of all collections. An updated version of these concurrency utilities was included in JDK 5.0 as of JSR 166.Īlmost all collections in Java are derived from the interface. ĭoug Lea later developed a concurrency package, comprising new Collection-related classes. Sun Microsystems chose not to use the ideas of JGL, because they wanted a compact framework, and consistency with C++ was not one of their goals. It reused many ideas and classes from Doug Lea's Collections package, which was deprecated as a result. The collections framework was designed and developed primarily by Joshua Bloch, and was introduced in JDK 1.2. To address the need for reusable collection data structures, 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 Library (STL). 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. History Ĭollection implementations in pre- JDK 1.2 versions of the Java platform included few data structure classes, but did not contain a collections framework. If the code is using Java SE7 or later versions, the developer can instatiate Collection as an ArrayList object by using the diamond operator Ĭollections are generic and hence reified, but arrays are not reified. On the other hand, if the developer instead declared a new instance of a Collection as ArrayList, 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 Collection as an ArrayList object. If the developer attempts to add a String to this Long object, the java program will throw an ArrayStoreException. For example, if a developer declares an Object object, and assigns the Object object to the value returned by a new Long instance with a certain capacity, no compile-time exception will be thrown. ![]() This can be considered an advantage of generic objects such as Collection when compared to arrays, because under circumstances, using the generic Collection instead of an array prevents run time exceptions by instead throwing a compile-time exception to inform the developer to fix the code. Ĭollections are generic and hence invariant, but arrays are covariant. Instead, Collections can hold wrapper classes such as, , or. Collections can grow and shrink in size automatically when objects are added or removed.Ĭollections cannot hold primitive data types such as int, long, or double. However, unlike arrays, Collections do not need to be assigned a certain capacity when instantiated. The collections framework provides both interfaces that define various collections and classes that implement them.Ĭollections and arrays are similar in that they both hold references to objects and they can be managed as a group. Īlthough referred to as a framework, it works in a manner of a library. The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |