Java Collections Framework
Java Collections Framework
https://www.tutorialspoint.com/java/java_collections.htm
Copyright © tutorialspoint.com
Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties
to store and manipulate groups of objects. Although these classes were quite useful, they lacked
a central, unifying theme. Thus, the way that you used Vector was different from the way that
you used Properties.
Towards this end, the entire collections framework is designed around a set of standard
interfaces. Several standard implementations such as LinkedList, HashSet, and TreeSet, of
these interfaces are provided that you may use as-is and you may also implement your own
collection, if you choose.
Interfaces − These are abstract data types that represent collections. Interfaces allow
collections to be manipulated independently of the details of their representation. In
object-oriented languages, interfaces generally form a hierarchy.
Implementations, i.e., Classes − These are the concrete implementations of the
collection interfaces. In essence, they are reusable data structures.
Algorithms − These are the methods that perform useful computations, such as searching
and sorting, on objects that implement collection interfaces. The algorithms 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. Although maps are not collections in the proper use of the term, but they are
fully integrated with collections.
The following legacy classes defined by java.util have been discussed in the previous chapter −
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.
The 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 through a collection, obtaining or removing elements. ListIterator
extends Iterator to allow bidirectional traversal of a list and the modification of elements.
This interface lets us sort a given collection any number of different ways. Also this interface can
be used to sort any instances of any class evenclasseswecannotmodify
Summary
The Java collections framework gives the programmer access to prepackaged data structures as
well as to algorithms for manipulating them.
A collection is an object that can hold references to other objects. The collection interfaces
declare the operations that can be performed on each type of collection.
The classes and interfaces of the collections framework are in package java.util.