The test was conducted using Java. The program consists of ten different files:
HashSetUtilTreeSetUtilLinkedHashSetUtilArrayListUtilLinkedListUtilArrayDequeUtilPriorityQueueUtilHashMapUtilTreeMapUtilLinkedHashMapUtil
Each sub-program is designed to evaluate the runtime performance of various methods (add, contain, remove, and clear) on the selected collections (HashSet, TreeSet, LinkedHashSet, ArrayList, LinkedList, ArrayDeque, PriorityQueue, HashMap, TreeMap, LinkedHashMap) in Java. The primary objective is to measure and compare the time taken for these operations to execute on these collections across multiple iterations. A mean run time was calculated to compare the collections with each other.
-
Creation of Collection Object:
- A method to create an object of the relevant collection containing random integers ranging up to 100,000.
-
Evolution Methods:
- Specific operations (
add,contain,remove, andclear) executed on the collection for 100 iterations. - The runtime for each iteration is recorded in an array.
- Average runtime for each operation is calculated.
- Specific operations (
-
Main Method:
- The above methods are called inside the
mainmethod. - The results are displayed in a formatted way.
- The above methods are called inside the
-
Test Conditions:
- Each collection was loaded with 100,000 items in the test.
- Time measurements were obtained at nanoseconds.
- The tests were performed using default initial capacity and load factor values.
-
Class Structure:
- Classes were created for each collection to obtain the runtime for operations.
- A test class was created to implement the specified methods for each collection to compare the runtimes.
HashSetUtil.javaTreeSetUtil.javaLinkedHashSetUtil.javaArrayListUtil.javaLinkedListUtil.javaArrayDequeUtil.javaPriorityQueueUtil.javaHashMapUtil.javaTreeMapUtil.javaLinkedHashMapUtil.java
To run the program, execute the main method in the test class.
-
HashSet:
- A hash function is used internally by the
HashSet add()method to identify which bucket the element should be placed in. It verifies unique elements and looks for duplication.
- A hash function is used internally by the
-
TreeSet:
- An internal Red-Black Tree is used by the
TreeSet add()method. A custom comparator or the elements' natural order is used to determine the sorting order of the elements.
- An internal Red-Black Tree is used by the
-
LinkedHashSet:
- The
LinkedHashSet add()method relies on a hash function, just like theHashSet add()method, but it additionally keeps a doubly-linked list to maintain the insertion order.
- The
-
ArrayList:
add()method inArrayListappends the element to the dynamic array. If the array is full, it should be resized. Then a new array is created, and the elements are copied to the new array.
-
LinkedList:
add()method inLinkedListadds a new node to the end of the linked list. It will update the references to maintain the structure of the linked list.
-
ArrayDeque:
add()method inArrayDequeadds the element to the end of the deque. If required, the internal array is resized.
-
PriorityQueue:
add()method inPriorityQueueinserts the element based on its priority. After each insertion, heap property is maintained.
-
HashMap:
put()method inHashMapinsert data after calculating the hash of the key to determine the bucket. If there's a hash collision, it resolves it using different techniques (separate chaining or open addressing). If necessary, it may need to resize the internal array.
-
TreeMap:
put()method inTreeMapadds the key-value pair to the Red-Black Tree. A sorted order is maintained based on the keys.
-
LinkedHashMap:
put()method inLinkedHashMapis similar to that ofHashMap. In addition to calculating the hash of the key, it also maintains a doubly linked list to preserve the order of insertion.
-
HashSet:
contains()method inHashSetuses the hash code of the element to check the corresponding bucket. Then equality checks are performed to see if the element is present.
-
TreeSet:
contains()method inTreeSetuses a self-balancing binary search, typically a Red-Black Tree to find the element based on its natural order or a custom comparator.
-
LinkedHashSet:
- Similar to the
contains()method inHashSet,LinkedHashSetalso uses the hash code to locate the specific bucket. Then it iterates through the linked list while checking for equality.
- Similar to the
-
ArrayList:
contains()method inArrayListiterates through the elements linearly. It performs equality checks until the element is found or the end of the list is reached.
-
LinkedList:
contains()method inLinkedListtraverses through the linked list starting from the head until the element is found or the end of the list is reached.
-
ArrayDeque:
contains()method inArrayDequeiterates over the internal array while performing equality checks until the element is found or the end of the deque is reached.
-
PriorityQueue:
contains()method inPriorityQueueiterates through the heap structure to find the element based on the priority.
-
HashMap:
containsKey()method inHashMapcalculates the hash of the key, determines the bucket, and then checks for equality within the entries of that bucket.
-
TreeMap:
containsKey()method inTreeMapchecks whether a key is present using a binary search algorithm based on the natural order or a custom comparator.
-
LinkedHashMap:
containsKey()method inLinkedHashMapuses the hash code to locate the bucket and then iterates through the linked list in that bucket.
-
HashSet:
remove()method inHashSetuses the hash code of the element to locate the corresponding bucket. It then iterates through the elements in that bucket and removes the relevant element.
-
TreeSet:
remove()method inTreeSetuses a binary search algorithm to find and remove the element based on its natural order or a custom comparator.
-
LinkedHashSet:
- Similar to
HashSet,remove()inLinkedHashSetcalculates the hash key and locates the relevant bucket. Then it iterates through the linked list in the bucket and removes the element based on equality.
- Similar to
-
ArrayList:
remove()method inArrayListuses linear search to find the index of the element and then removes it. After removing, the subsequent elements are shifted to fill the gap.
-
LinkedList:
LinkedList'sremove()method traverses through the linked list to find the node that contains the element. Then it adjusts the references to remove the node.
-
ArrayDeque:
remove()method inArrayDequeremoves the element from the internal array and shifts the elements to fill the gap if necessary.
-
PriorityQueue:
remove()method inPriorityQueueremoves and returns the element with the highest priority. The priority within the queue is then maintained by reorganizing the heap.
-
HashMap:
remove()method inHashMapuses the hash code of the key to locate the bucket. It then iterates through the elements in the bucket and removes the relevant element.
-
TreeMap:
remove()method inTreeMapuses a binary search algorithm to find and remove the element based on the natural order or a custom comparator.
-
LinkedHashMap:
remove()method inLinkedHashMapuses the hash code to locate the bucket and then iterates through the linked list in the bucket to remove the relevant element.
-
HashSet:
clear()method inHashSetinvolves resetting the internal array of buckets to an initial state. Each bucket is set to null.
-
TreeSet:
clear()method inTreeSetresets the root of the tree to null, effectively removing all elements.
-
LinkedHashSet:
clear()method inLinkedHashSetresets the head and tail of the linked list to null, while maintaining the linked structure for preserving insertion order.
-
ArrayList:
clear()method inArrayListsets the size of the dynamic array to zero, effectively removing all elements. Any references to objects might still exist in memory, but the list itself becomes empty.
-
LinkedList:
clear()method inLinkedListremoves all elements individually and updates the linked structure while iterating through the list.
-
ArrayDeque:
clear()method inArrayDequesets the size of #Time complexity of operations
| Collection | Method | Add (Avg case) | Add (Worst case) | Contain (Avg case) | Contain (Worst case) | Remove (Avg case) | Remove (Worst case) | Clear (Avg case and Worst case) |
|---|---|---|---|---|---|---|---|---|
| HashSet | O(1) | O(n) | O(1) | O(n) | O(1) | O(n) | O(n) | O(n) |
| TreeSet | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) |
| LinkedHashSet | O(1) | O(n) | O(1) | O(n) | O(1) | O(n) | O(n) | O(n) |
| ArrayList | O(1) | O(n) | O(n) | O(n) | O(n) | O(n) | O(1) | O(1) |
| LinkedList | O(1) | O(1) | O(n) | O(n) | O(n) | O(n) | O(1) | O(1) |
| ArrayDeque | O(1) | O(1) | O(n) | O(n) | O(n) | O(n) | O(1) | O(1) |
| PriorityQueue | O(log n) | O(log n) | O(n) | O(n) | O(n) | O(n) | O(n) | O(n) |
| HashMap | O(1) | O(n) | O(1) | O(n) | O(1) | O(n) | O(n) | O(n) |
| TreeMap | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) | O(log n) |
| LinkedHashMap | O(1) | O(n) | O(1) | O(n) | O(1) | O(n) | O(log n) | O(n) |