Chapter 10 Java Collections Framework
Java Collections Framework provides a well-designed set of interfaces and classes that support operations on a collection of objects. These classes and interfaces are reusable, making it easy for developers to create and manipulate collections in a consistent manner.
Overview of Collections Framework¶
The Java Collections Framework is a unified architecture for representing and manipulating collections. It includes implementations for various types of collections, including sets, lists, queues, and maps, which can all hold data in different ways.
The core collection interfaces introduced with the Collections Framework are:
-
Collection: The root of the collection hierarchy. A collection represents a group of objects known as its elements.
-
Set: A collection that cannot contain duplicate elements. It models the mathematical set abstraction.
-
List: An ordered collection and can contain duplicate elements. You can access elements by their integer index (position).
-
Queue: Typically used to model a line of items or objects, queued in a particular order to be processed.
-
Map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
The Collections Framework also includes implementations for each of these interfaces, such as LinkedList, ArrayList, HashSet, LinkedHashSet, HashMap, and TreeMap, among others.
Lists, Sets, Maps, Queues in Java¶
Lists in Java are ordered collections, also known as sequences. Lists may contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index.
-
ArrayList:
ArrayListis a resizable-array implementation of the List interface. It has a closer similarity to Array, but it's resizable. It's better for storing and accessing data, as it provides random access to elements using the index, which is faster in memory. It's not so efficient for manipulation, such as deletion or insertion in the middle of the list, because it needs to update the index of subsequent elements. -
LinkedList:
LinkedListis a doubly-linked list implementation of the List and Deque interfaces. UnlikeArrayList, it has a dynamic data structure (linked nodes), which is good for frequent insertion/deletion, because it just needs to update the links of adjacent nodes. It's not as efficient for accessing elements, because it has to iterate over the nodes sequentially from the start or end of the list.
Sets in Java store a collection of distinct elements — no duplicates are allowed.
-
HashSet:
HashSetis implemented using a hash table, which provides constant time performance for the basic operations (add, remove, contains and size). It does not guarantee any specific order of its elements. -
LinkedHashSet:
LinkedHashSetis a hash table and linked list implementation of the Set interface. It maintains a doubly-linked list running through all of its elements, which defines the order in which elements were inserted into the set (insertion-order). -
TreeSet:
TreeSetis a NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time.
Maps in Java store key-value pairs.
-
HashMap:
HashMapis implemented as a hash table, and there is no ordering on keys or values. -
LinkedHashMap:
LinkedHashMapis a hash table and linked list implementation of the Map interface, with predictable iteration order. It maintains a doubly-linked list running through all of its entries. -
TreeMap:
TreeMapis a Red-Black tree based NavigableMap implementation. It's sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time.
Queues in Java are typically used to model a line of items or objects, queued in a particular order to be processed.
-
PriorityQueue:
PriorityQueueis a class which is implemented using Heap data structure. It does not orders the elements in FIFO manner. It is unbounded but we can specify the initial capacity. -
ArrayDeque:
ArrayDequeclass is a resizable array implementation of the Deque interface. Array deques have no capacity restrictions; they grow as necessary to support usage. -
LinkedList:
LinkedListimplements both List and Deque interfaces. It can be used as both a queue (FIFO) and stack (LIFO).