logo

Java Collections Framework


Show

Previous to Java 2, Java offered ad hoc classes such as lexicon, Vector, heap, and Properties to amass and influence groups of entities. Although these objects were useful, they required a middle, unifying theme. Thus, the technique that you exercised Vector was diverse from the means that you utilized Properties.

The compilations structure was planned to meet numerous goals, such as −

  • The structure had to be high-presentation. The completions for the primary compilations (dynamic arrays, confectioned lists, trees, and hashtables) were to be exceedingly resourceful.
  • The structure had to consent to diverse types of sets to job in a similar way and with a lofty degree of interoperability.
  • The structure had to expand and/or acclimatize an anthology easily.

Towards this end, the frame of the complete collection is meant around a set of standard lines. Numerous typical implementations such as LinkedList, HashSet, and TreeSet, of these lines, are if you might employ as-is and you may implement your possess collection if you decide.

A compilations frame is a combined architecture for instead of and manipulating compilations. Entire collections frameworks enclose the subsequent −

  • Interfaces − these are conceptual data categories that signify collections. Interfaces permit anthologies to be maneuvered separately of the features of their symbol. In object-oriented languages, lines usually form a ladder.
  • Completions, i.e., Classes − these are the real completions of the set interfaces. At the core, they are reusable data arrangements.
  • Algorithms − these are the techniques that carry out useful computations, such as penetrating and categorization, on objects that put into practice collection interfaces. The algorithms are assumed to be polymorphic which means, the same technique can be employed on numerous unlike implementations of the fitting collection interface.

In addition to collections, the framework describes quite a few map lines and classes. Maps amass key/worth pairs. Although maps are not sets in the correct utilization of the expression, yet they are copiously included with collections.

The Collection Interfaces

The collections framework defines various interfaces. This section offers an overview of every interface −

Sr.No.Interface & Description
1The Collection Interface

This enables you to work with groups of objects; it is at the top of the collections hierarchy.

2The List Interface

This extends Collection and an instance of List stores an ordered collection of elements.

3The Set

This extends Collection to handle sets, which must contain unique elements.

4The SortedSet

This extends Set to handle sorted sets.

5The Map

This maps unique keys to values.

6The Map.Entry

This describes an element (a key/value pair) in a map. This is an inner class of Map.

7The SortedMap

This extends Map so that the keys are maintained in ascending order.

8The Enumeration

This is legacy interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superseded by Iterator.

The Collection Classes

Java applies a set of typical collection classes that realize Collection interfaces. A few of the classes supply filled executions that can be utilized as-is and others are conceptual classes, offering skeletal implementations that are employed as beginning points for developing tangible collections.

The typical collection classes are abridged in the subsequent table −

Sr.No.Class & Description
1

AbstractCollection

Implements most of the Collection interface.

2

AbstractList

Extends AbstractCollection and implements most of the List interface.

3

AbstractSequentialList

Extends AbstractList for use by a collection that uses sequential rather than random access of its elements.

4LinkedList

Implements a linked list by extending AbstractSequentialList.

5ArrayList

Implements a dynamic array by extending AbstractList.

6

AbstractSet

Extends AbstractCollection and implements most of the Set interface.

7HashSet

Extends AbstractSet for use with a hash table.

8LinkedHashSet

Extends HashSet to allow insertion-order iterations.

9TreeSet

Implements a set stored in a tree. Extends AbstractSet.

10

AbstractMap

Implements most of the Map interface.

11HashMap

Extends AbstractMap to use a hash table.

12TreeMap

Extends AbstractMap to use a tree.

13WeakHashMap

Extends AbstractMap to use a hash table with weak keys.

14LinkedHashMap

Extends HashMap to allow insertion-order iterations.

15IdentityHashMap

Extends AbstractMap and uses reference equality when comparing documents.

The AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList, and AbstractMap classes provide skeletal implementations of the core collection interfaces, to minimize the effort required to implement them.

The following legacy classes are defined by java.util has been discussed in the previous chapter −

Sr.No.Class & Description
1Vector

This implements a dynamic array. It is similar to ArrayList, but with some differences.

2Stack

Stack is a subclass of Vector that implements a standard last-in, first-out stack.

3Dictionary

Dictionary is an abstract class that represents a key/value storage repository and operates much like Map.

4Hashtable

Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.

5Properties

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.

6BitSet

A BitSet class creates a special type of array that holds bit values. This array can increase in size as needed.

The Collection Algorithms

The collections frame classifies some algorithms that can be useful to collections and maps. These algorithms are described as motionless methods within the Collections class.

Several of the methods can throw a ClassCastException, which occurs when an attempt is made to compare incompatible types, or an UnsupportedOperationException, which occurs when an attempt is made to modify an unmodifiable collection.

Collections define three static variables: EMPTY_SET, EMPTY_LIST, and EMPTY_MAP. All are immutable.

Sr.No.Algorithm & Description
1The Collection Algorithms

Here is a list of all the algorithm implementations.

How to Use an Iterator?

Frequently, you will desire to cycle throughout the constituents in an anthology. For instance, you might desire to show each element.

The easiest technique to do this is to use an iterator, which is a thing that equipment either the Iterator or the ListIterator line.

Iterator allows you to round through a compilation, getting or removing elements. ListIterator expands Iterator to permit bidirectional traversal of an inventory and the change of basics.

Sr.No.Iterator Method & Description
1Using Java Iterator

Here is a list of all the methods with examples provided by Iterator and ListIterator interfaces.

How to Use a Comparator?

Both TreeSet and TreeMap store basics in a classed order. But, it is the comparator that describes exactly what ensued order means.

This border lets us class a known collection any figure of dissimilar ways. Also, this line can be utilized to sort any case in points of any class (even classes we never modify).

Sr.No.Iterator Method & Description
1Using Java Comparator

Here is a list of all the methods with examples provided by Comparator Interface.

Summary

The Java compilations structure offers the programmer admittance to prepackaged information arrangements as well as to algorithms for manipulating them.

A set is an article that can clutch orientations to other entities. The collection lines state the procedures that can be executed on each type of anthology.

The classes and lines of the framework of the album are in wrap-up java.util.

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.