0% found this document useful (0 votes)
40 views1 page

Java Collections Framework Cheat Sheet

Uploaded by

sou.dutta.14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views1 page

Java Collections Framework Cheat Sheet

Uploaded by

sou.dutta.14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Collections Framework - Concise Cheat Sheet

Collection (Root Interface)


- boolean add(E e) - boolean remove(Object o) - boolean contains(Object o) - int
size() - void clear() - Iterator iterator()

List (ArrayList, LinkedList, Vector)


Syntax: List list = new ArrayList<>(); - void add(int index, E element) - E get(int
index) - E set(int index, E element) - E remove(int index) - boolean contains(Object
o)

Stack
Syntax: Stack stack = new Stack<>(); - E push(E item) - E pop() - E peek() - boolean
empty() - int search(Object o)

Set (HashSet, LinkedHashSet, TreeSet)


Syntax: Set set = new HashSet<>(); - boolean add(E e) - boolean remove(Object o) -
boolean contains(Object o) - int size() - void clear()

Queue / Deque (LinkedList, PriorityQueue, ArrayDeque)


Syntax: Queue q = new LinkedList<>(); - boolean offer(E e) - E poll() - E peek() -
void addFirst(E e) - void addLast(E e) - E removeFirst() - E removeLast()

Map (HashMap, LinkedHashMap, TreeMap, Hashtable)


Syntax: Map map = new HashMap<>(); - V put(K key, V value) - V get(Object key) -
boolean containsKey(Object key) - boolean containsValue(Object value) - V
remove(Object key) - Set keySet() - Collection values() - Set> entrySet()

StringBuilder / StringBuffer
Syntax: StringBuilder sb = new StringBuilder(); - StringBuilder append(String s) -
StringBuilder insert(int offset, String s) - StringBuilder delete(int start, int
end) - StringBuilder replace(int start, int end, String s) - StringBuilder reverse()
- int capacity() - void ensureCapacity(int minCapacity)

Utility Classes
Collections: - [Link](list) - [Link](list) -
[Link](list) Arrays: - [Link](array) - [Link](array) -
[Link](array, key)

Common questions

Powered by AI

ArrayList and LinkedList are both List implementations in Java Collections. ArrayList uses a dynamic array, providing constant-time positional access, making it faster for index-based access. However, insertion and deletion are costly, requiring shifting elements. LinkedList, on the other hand, uses a doubly-linked list, which makes element insertion or deletion operations more efficient, particularly at the beginning or end, but accessing an element takes linear time. Selection between them depends on usage requirements; frequent add/delete operations favor LinkedList, while frequent access operations favor ArrayList .

The Iterator interface facilitates navigation through collections by providing methods like `hasNext()`, `next()`, and `remove()`. It abstracts the traversal process, enabling developers to easily iterate over elements without concerning themselves with underlying collection structure details. This reduces code complexity and enhances efficiency by offering a standardized mechanism for element access and removal within loops, promoting cleaner, maintainable, and flexible code .

A PriorityQueue can be used to implement scheduling algorithms by maintaining a priority heap structure, where elements are ordered according to their natural ordering or by a comparator provided at queue construction time. This structure ensures that the head of the queue is the least element with respect to the specified ordering, allowing efficient extraction of tasks with the highest priority. This is particularly useful in scenarios such as task scheduling, CPU load balancing, and bandwidth management, as it efficiently prioritizes tasks based on urgency or defined hierarchy .

The Arrays utility class offers methods like `asList` and `sort` for transforming arrays and ordering elements. `asList` creates a fixed-size list backed by the specified array, allowing for easy iteration and processing of array data. `sort` uses an optimized quicksort or mergesort algorithm for primitives and objects, respectively, providing O(n log n) time complexity. These features are critical in scenarios demanding high-efficiency data handling and quick integration of static arrays into dynamic environments such as GUIs or data pipelines .

HashMap, LinkedHashMap, and TreeMap are Map implementations with key differences. HashMap offers constant time for get and put operations if hash functions distribute elements well, but it does not guarantee any order. LinkedHashMap maintains a doubly-linked list, thereby retaining insertion order, and can even operate in access order if desired. TreeMap implements the NavigableMap interface and sorts elements based on natural ordering of keys or by comparator, resulting in log(n) time complexity for operations. Selection often depends on the need for order guarantees or performance efficiency .

StringBuilder operations such as `append`, `insert`, `delete`, and `replace` enable mutable string manipulation by directly modifying the character sequence they manage. The `append` method extends the sequence by adding additional strings or characters. `insert` allows characters to be added at a specified position, shifting subsequent elements. `delete` removes a range of characters, and `replace` substitutes a subsequence with another string. These operations provide flexibility and efficiency, especially when numerous modifications are expected, as opposed to immutable string handling offered by the String class .

A Queue implemented using LinkedList follows First-In-First-Out (FIFO) ordering by enqueueing elements at the tail and dequeueing from the head. It offers a linked list structure which ensures that add and remove operations occur in constant time, preserving order of insertion. This makes it ideal for task scheduling, where processes must be executed in the sequence they are received, ensuring efficient and predictable handling of tasks such as print spooling or request processing .

HashSet, LinkedHashSet, and TreeSet are Set implementations that ensure element uniqueness. HashSet offers constant-time performance for basic operations like add, remove, and contains, but does not maintain any order. LinkedHashSet maintains insertion order using a linked list running through all entries. TreeSet stores elements in a red-black tree structure, ordering them according to natural ordering or a specified comparator, thus providing log(n) time cost for operations. The choice among them depends on whether order maintenance or speed of operations is prioritized .

Methods in the Collections utility class, including `sort`, `reverse`, and `shuffle`, provide essential algorithmic operations on lists. The `sort` method uses a tuned merge sort algorithm, offering n log(n) time complexity, to arrange elements in ascending order. `reverse` inverses the order of a list, and `shuffle` randomizes the list's order, valuable in applications like simulations where random ordering is essential. The utility methods ensure that complex algorithmic manipulations on collections are simplified and optimized without the need for extensive custom code .

Stack operations in Java Collections Framework use the methods `push`, `pop`, and `peek` to maintain Last-In-First-Out (LIFO) ordering. `push` adds an element to the top of the stack, `pop` removes the top element, and `peek` retrieves it without removing. This ordering is useful in use cases such as undo mechanisms in text editors, reversing data, or parsing syntax trees in compilers .

You might also like