Java Collections Framework: The Collection Interfaces

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

JAVA COLLECTIONS FRAMEWORK

http://www.tuto rialspo int.co m/java/java_co lle ctio ns.htm


Co pyrig ht tuto rials po int.co m

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.

The Collection Interfaces:


T he collections framework defines several interfaces. T his section provides an overview of each interface:

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.

The Collection Classes:


Java provides a set of standard collection classes that implement Collection interfaces. Some of the classes provide full implementations that can be used as-is and others are abstract class, providing skeletal implementations that are used as starting points for creating concrete collections. T he standard collection classes are summarized in the following table:

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

Extends AbstractMap and uses reference equality when comparing documents.

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

The Collection Alg orithms:


T he collections framework defines several alg orithms that can be applied to collections and maps. T hese alg orithms are defined as static methods within the Collections class. Several of the methods can throw a ClassCastExc eption, which occurs when an attempt is made to compare incompatible types, or an UnsupportedO perationExc eption, which occurs when an attempt is made to modify an unmodifiable collection. Collections define three static variables: EMPT Y_SET , EMPT Y_LIST , and EMPT Y_MAP. All are immutable.

SN 1

Alg orithms with Desc ription T he Collection Alg orithms Here is a list of all the alg orithm implementation.

How to use an Iterator ?


Often, you will want to cycle throug h the elements in a collection. For example, you mig ht want to display each element. T he easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface. Iterator enables you to cycle throug h a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list and the modification of elements.

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.

How to use a Comparator ?


Both T reeSet and T reeMap store elements in sorted order. However, it is the comparator that defines precisely what sorted order means. T his interface lets us sort a g iven collection any number of different ways. Also this interface can be used to sort any instances of any class (even classes we cannot modify).

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.

You might also like