Java Collections Framework: The Collection Interfaces
Java Collections Framework: The Collection Interfaces
Java Collections Framework: The Collection Interfaces
Prior to Java 2, Java provided ad hoc classes such as Dic tionary, Vec tor, Stac k, and Properties to store and manipulate g roups of objects. Althoug h these classes were quite useful, they lacked a central, unifying theme. T hus, the way that you used Vector was different from the way that you used Properties. T he collections framework was desig ned to meet several g oals. T he framework had to be hig h-performance. T he implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) are hig hly efficient. T he framework had to allow different types of collections to work in a similar manner and with a hig h deg ree of interoperability. Extending and/or adapting a collection had to be easy. T owards this end, the entire collections framework is desig ned around a set of standard interfaces. Several standard implementations such as LinkedList, HashSet, and T reeSet, of these interfaces are provided that you may use as-is and you may also implement your own collection, if you choose. A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following : Interfac es: T hese are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented lang uag es, interfaces g enerally form a hierarchy. Implementations, i.e., Classes: T hese are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. Alg orithms: T hese are the methods that perform useful computations, such as searching and sorting , on objects that implement collection interfaces. T he alg orithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Althoug h maps are not collections in the proper use of the term, but they are fully integ rated with collections.
SN 1 2 3 4 5 6
Interfac es with Desc ription T he Collection Interface T his enables you to work with g roups of objects; it is at the top of the collections hierarchy. T he List Interface T his extends Collec tion and an instance of List stores an ordered collection of elements. T he Set T his extends Collection to handle sets, which must contain unique elements T he SortedSet T his extends Set to handle sorted sets T he Map T his maps unique keys to values. T he Map.Entry T his describes an element (a key/value pair) in a map. T his is an inner class of Map.
7 8
T he SortedMap T his extends Map so that the keys are maintained in ascending order. T he Enumeration T his is leg acy interface and defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. T his leg acy interface has been superceded by Iterator.
SN 1 2 3
Classes with Desc ription Abstrac tCollec tion Implements most of the Collection interface. Abstrac tList Extends AbstractCollection and implements most of the List interface. Abstrac tSequentialList Extends AbstractList for use by a collection that uses sequential rather than random access of its elements. LinkedList Implements a linked list by extending AbstractSequentialList. ArrayList Implements a dynamic array by extending AbstractList. Abstrac tSet Extends AbstractCollection and implements most of the Set interface. HashSet Extends AbstractSet for use with a hash table. LinkedHashSet Extends HashSet to allow insertion-order iterations. T reeSet Implements a set stored in a tree. Extends AbstractSet. Abstrac tMap Implements most of the Map interface. HashMap Extends AbstractMap to use a hash table. T reeMap Extends AbstractMap to use a tree. WeakHashMap Extends AbstractMap to use a hash table with weak keys. LinkedHashMap Extends HashMap to allow insertion-order iterations. IdentityHashMap
4 5 6 7 8 9 10 11 12 13 14 15
T he AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap classes provide skeletal implementations of the core collection interfaces, to minimize the effort required to implement them. T he following leg acy classes defined by java.util have been discussed in previous tutorial:
SN 1 2 3
Classes with Desc ription Vector T his implements a dynamic array. It is similar to ArrayList, but with some differences. Stack Stack is a subclass of Vector that implements a standard last-in, first-out stack. Dictionary Dictionary is an abstract class that represents a key/value storag e repository and operates much like Map. Hashtable Hashtable was part of the orig inal java.util and is a concrete implementation of a Dictionary. Properties Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String . BitSet A BitSet class creates a special type of array that holds bit values. T his array can increase in size as needed.
4 5
SN 1
Alg orithms with Desc ription T he Collection Alg orithms Here is a list of all the alg orithm implementation.
SN 1
Iterator Methods with Desc ription Using Java Iterator Here is a list of all the methods with examples provided by Iterator and ListIterator interfaces.
SN 1
Iterator Methods with Desc ription Using Java Comparator Here is a list of all the methods with examples provided by Comparator Interface.
Summary:
T he Java collections framework g ives the prog rammer access to prepackag ed data structures as well as to alg orithms for manipulating them. A collection is an object that can hold references to other objects. T he collection interfaces declare the operations that can be performed on each type of collection. T he classes and interfaces of the collections framework are in packag e java.util.