Java Reference
In-Depth Information
equals()
and
hashCode()
methods of your mutable class very carefully. You
must return the same value from the
hashCode()
method of the object of the mutable class.
Otherwise, you will lose track of the objects of your mutable class in hash-based collections.
If a mutable class has some part of its state that is immutable, use those immutable parts of
the class to compute its hash code value so that the hash code value does not change for an
object of the mutable class. As a last resort, which is not recommended, consider returning a
constant integer from the
hashCode()
method of your mutable class.
Make sure that the contracts for the
equals()
and
hashCode()
methods are fulfilled.
•
Implement the
Summary
A collection is a group of objects. Java provides a Collections Framework containing several interfaces and classes
for working with a wide range of collection types such as lists, queues, sets, and maps. The Collections Framework
provides an interface to represent a specific type of collection. Each interface in the framework has at least one
implementation class. Collection-related interfaces and classes are in the
java.util
package. Collection classes to be
used in multi-threaded programs where synchronization is needed are in the
java.util.concurrent
package.
The Collections Framework contains a
Collection
interface that is the root for most of the collections. The
Collection
interface contains most of the methods used with all types of collection (except for the
Map
-based collections). The interface
provides methods for adding elements, removing elements, knowing the size of the collection, etc. Specific subinterfaces of
the Collection interface provide additional methods to work with the specific type of collections.
The Collections Framework provides a uniform way for traversing elements of all types of collections using
iterators. An instance of the
Iterator i
nterface represents an iterator. All collections support traversing their
elements using the for-each loop and a
forEach()
method.
In mathematics, a set is a collection of unordered unique elements. An instance of the
Set
interface represents a
set in the Collections Framework.
HashSet
is the implementation class for the mathematical set.
An instance of the
SortedSet
represents an ordered unique set.
TreeSet
is the implementation class for the
SortedSet
interface. Elements in a sorted set can be sorted in natural order or in a custom order using a
Comparator
.
A queue is a collection of objects that are used for processing objects one at a time. Objects enter the queue from
one end and exit the queue from another end. The
Queue
interface in the Collections Framework represents a queue.
The Collections Framework provides several implementation classes for the
Queue
interface to support different types
of queues, such as a simple queue, blocking queue, priority queue, delay queue, etc.
A list is an ordered collection of objects. An instance of the
List
interface represents a list in the Collections
Framework.
ArrayList
and
LinkedList
are two implementation classes for the
List
interface that are backed up
by an array and a linked list, respectively. Each element in the list has an index that starts from 0. The
List
interface
provides methods that let you access its elements sequentially or randomly using indexes of the elements. The
Collections Framework supports only a dense list; that is, there cannot be a gap between two elements in the list.
A map is another type of collection that stores key-value pairs. Keys in a map must be unique. An instance of the
Map
interface represents a map in the Collections Framework.
HashMap
is the simple implementation class for the
Map
interface. The Collections Framework also supports sorted, navigable, and concurrent maps. A sorted map stores all
key-value pairs sorted based on keys. An instance of the
SortedMap
interface represents a sorted map.
TreeMap
is the
implementation class for the
SortedMap
interface. An instance of the
NavigableMap
and
ConcurrentMap
represent a
navigable map and concurrent map, respectively.
The Collections Framework contains a utility class called
Collections
that contains only static methods.
Methods in this class let you apply different types of algorithms to a collection—for example, shuffling elements in
a collection, rotating its elements, sorting elements of a list, etc. The class also provides methods to obtain different
views of collections, such as read-only view, synchronized view, unmodifiable view, etc.
A hash-based collection uses buckets to store its elements. The number of buckets is determined based on the
number of elements in the collection and the required performance. When an element is added to the collection, the
element's hash code is used to determine the bucket in which the element will be stored. A reverse process is used
when an element is searched in the collection. Hash-based collections provide faster element storage and retrieval.