You are on page 1of 2

Java Collections Framework.

Collection. A collection is an object that represents a group or collection of objects. A collections framework is a unified architecture for representation and manipulation of such collections. The collections framework consists of collection interfaces and some general purpose implementations of these interfaces.

More in ebook

Collection Interfaces. More in ebook There are six collection interfaces. The most basic interface is Collection. Three interfaces that extend Collection are Set, List, and SortedSet. The other two collection interfaces, Map and SortedMap, do not extend Collection, as they represent mappings rather than true collections. Collection interfaces can be categorized as ... Set is an interface, which models mathematical abstraction. Set provides an API for a collection that contains no duplicate elements. SortedSet interface extends this interface for ordered collection of unique elements. List is an interface for ordered collection. Unlike sets, lists typically allow duplicate elements. ArrayList, Vector, LinkedList are the some of the classes which implement this interface. Map is an interface for a collection of key-value pairs. It is not an ordered collection. Repetitions are not allowed. It also provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. Interface SortedMAp extends this interface for ordered key-value pairs. Classes HashMap, Hashtable are some implementations of this interface.

Collections and hashcode() method. While managing the objects in collections, a collection often compares the objects. This usually done with equals() method. Therefore, if you wish that the logically similar objects of your class should behave properly while stored in collection, it is sometimes necessary to override the equals() method in your class. In such case equals() method should be overridden such that it will do "Logical Equality" test between two objects. You must override hashCode in every class that overrides equals(). Equal objects must have equal hash codes. In other words, If two objects are equal according to the equals(Object)method, then calling the hashCode method on each of the two objects must produce the same integer result. More
in ebook

Implementation of Collection interfaces. Storage associated with any collection can be implemented using different data structures such as arrays, linked lists, tree, and hashtable. Each data structure has some benefits as well as some drawbacks. A correct choice of data structure can be made based on the nature of data and the requirements. For example, The data is of fixed size, does not change much. In addition, if it is accessed frequently and needs to be ordered then perhaps arrays is a good choice.

Benefits and constraints of using different data structures.


Array Benefits Constraints

Data access is fast. Good for ordered data, which is not changed or searched frequently.

Inefficient if number of elements grow. Inefficient if an element to be inserted in middle of collection. Provides no special search mechanism.

Linked List Benefits Constraints

Allows efficient inserts/delete at any location Allows arbitrary growth of collection. Applying order to the elements is easy.

Slower while accessing elements by index. No special search mechanism is provided.

Tree Benefits Constraints

Easy addition/deletion in middle. Allows arbitrary growth. A better and efficient search mechanism.

Ordering is peculiar and some comparison mechanism is required. Searching is not efficient for unevenly distributed data.

Hashtable Benefits Constraints

Efficient searching. Good access mechanism. Allows arbitrary growth of collection.

Not good for small data set because of overheads. Overhead for storing keys for the values in collection. Overhead of hashing scheme.

You might also like