Collections Diagrams
Collections Diagrams
- Provide a set of interfaces and classes to implement various data structures and algorithms.
- Manipulation operations: storing data, searching, sorting, insertion, deletion, and updating of data.
# Arrays Collection
Arrays can hold both object and Collection can hold only object types but not primitive datatypes such
6
primitive data type . as int, long, short, etc.
ArrayList:
iterator() - returns iterator object that can be used to sequentially access elements of lists
Set Operations
The Java Set interface allows us to perform basic mathematical set operations like union, intersection, and subset.
Union - to get the union of two sets x and y, we can use x.addAll(y)
Intersection - to get the intersection of two sets x and y, we can use x.retainAll(y)
Subset - to check if x is a subset of y, we can use y.containsAll(x)
Queue:
add() - Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
offer() - Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
element() - Returns the head of the queue. Throws an exception if the queue is empty.
peek() - Returns the head of the queue. Returns null if the queue is empty.
remove() - Returns and removes the head of the queue. Throws an exception if the queue is empty.
poll() - Returns and removes the head of the queue. Returns null if the queue is empty.
Deque:
removeFirst() - returns and removes the first element from the array deque (equivalent to remove())
removeLast() - returns and removes the last element from the array deque
pollFirst() - returns and removes the first element of the array deque (equivalent to poll()
pollLast() - returns and removes the last element of the array deque
Sorted Set:
comparator() - returns a comparator that can be used to order elements in the set
first() - returns the first element of the set
last() - returns the last element of the set
headSet(element) - returns all the elements of the set before the specified element
tailSet(element) - returns all the elements of the set after the specified element including the specified element
subSet(element1, element2) - returns all the elements between the element1 and element2 including element1