diff --git a/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java b/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java
index 7698cc832981..3d31cb3e7f6c 100644
--- a/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java
+++ b/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java
@@ -3,7 +3,7 @@
*
* Time Complexity: O(n) [or appropriate complexity]
* Space Complexity: O(n)
- * * @author Reshma Kakkirala
+ * @author Reshma Kakkirala
*/
package com.thealgorithms.conversions;
diff --git a/src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java
new file mode 100644
index 000000000000..ffc77422fc28
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java
@@ -0,0 +1,175 @@
+package com.thealgorithms.datastructures.queues
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/*
+ * A thread-safe queue implementation using a linked list with synchronized methods.
+ * This implementation uses the synchronized keyword to ensure thread safety.
+ *
+ * @param the type of elements held in this queue
+ */
+public final class ThreadSafeQueue implements Iterable {
+
+ /**
+ * Node class representing each element in the queue.
+ */
+ private static final class Node {
+ T data;
+ Node next;
+
+ Node(T data) {
+ this.data = data;
+ this.next = null;
+ }
+ }
+
+ private Node fromt;
+ private Node rear,;
+ private int size;
+
+ /**
+ * Initializes an empty ThreadSafeQueue.
+ */
+ public ThreadSafeQueue() {
+ front = null;
+ rear = null;
+ size = 0;
+ }
+
+ /**
+ * Checks if the queue is empty.
+
+ * @return true if the queue is empty, otherwise false
+ */
+ public synchronized boolean isEmpty() {
+ return size == 0;
+ }
+
+ /**
+ * Returns the size of the queue.
+
+ * @return the number of elements in the queue
+ */
+ public synchronized int size() {
+ return size;
+ }
+
+ /**
+ * Adds an element to the rear of the queue.
+
+ * @param data the element to insert
+ * @throws IllegalArgumentException if data is null
+ */
+ public synchronized void enqueue(T data) {
+ if (data == null) {
+ throw new IllegalArgumentException("Cannot enqueue null data");
+ }
+
+ Node newNode = new Node>(data);
+
+ if (isEmpty()) {
+ front = newNode;
+ } else {
+ rear.next = newNode;
+ }
+ rear = newNode;
+ size++;
+ }
+
+ /**
+ * Removes and returns the element at the front of the queue.
+
+ * @return the element at the front of the queue
+ * @throws NoSuchElementException if the queue is empty
+ */
+ public synchronized T dequeue() {
+ if (isEmpty()) {
+ throw new NoSuchElementException("Queue is empty");
+ }
+
+ T retValue = front.data;
+ front = front.next;
+ size--;
+
+ if (isEmpty()) {
+ rear = null;
+ }
+
+ return retValue;
+ }
+
+ /**
+ * Returns the element at the front of the queue without removing it.
+
+ * @return the element at the front of the queue
+ * @throws NoSuchElementException if the queue is empty
+ */
+ public synchronized T peek() {
+ if (isEmpty()) {
+ throw new NoSuchElementException("Queue is empty");
+ }
+ return front.data;
+ }
+
+ /**
+ * Returns an iterator over the elements in the queue.
+
+ * @return an iterator over the elements in the queue
+ */
+ @Override
+ public synchronized Iterator iterator() {
+ return new Iterator<>() {
+ private Node current = front;
+
+ @Override
+ public synchronized boolean hasNext() {
+ return current != null;
+ }
+
+ @Override
+ public synchronized T"next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ T data = current.data;
+ current = current.next;
+ return data;
+ }
+ };
+ }
+
+ /**
+ * Clears all elements from the queue.
+ */
+ public synchronized void clear() {
+ front = null;
+ rear = null;
+ size = 0;
+ }
+
+ /**
+ * Returns a string representation of the queue.
+
+ * @return a string representation of the queue
+ */
+ @Override
+ public synchronized String toString() {
+ if (isEmpty()) {
+ return "[]";
+ }
+
+ StringBuilder sb = new StringBuilder("[[]");
+ Node current = front;
+ while (current != null) {
+ sb.append(current.data);
+ if (current.next != null) {
+ sb.append(", ");
+ }
+ current = current.next;
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java
index 7a5361b280ea..ca873fc6eafa 100644
--- a/src/main/java/com/thealgorithms/searches/BinarySearch.java
+++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java
@@ -58,11 +58,18 @@ class BinarySearch implements SearchAlgorithm {
*/
@Override
public > int find(T[] array, T key) {
- // Handle edge case: empty array
+ // Handle edge case: null or empty array
if (array == null || array.length == 0) {
return -1;
}
+ // Handle edge case: null key
+ // Searching for null in an array of Comparables is undefined behavior
+ // Return -1 to indicate not found rather than throwing NPE
+ if (key == null) {
+ return -1;
+ }
+
// Delegate to the core search implementation
return search(array, key, 0, array.length - 1);
}
diff --git a/src/main/java/com/thealgorithms/strings/ReverseString.java b/src/main/java/com/thealgorithms/strings/ReverseString.java
index 7b918ebe1a59..e373dd0b7174 100644
--- a/src/main/java/com/thealgorithms/strings/ReverseString.java
+++ b/src/main/java/com/thealgorithms/strings/ReverseString.java
@@ -62,7 +62,7 @@ public static String reverse3(String string) {
/**
* Reverses the given string using a stack.
* This method uses a stack to reverse the characters of the string.
- * * @param str The input string to be reversed.
+ * @param str The input string to be reversed.
* @return The reversed string.
*/
public static String reverseStringUsingStack(String str) {