Java Utililty Methods Queue Usage

List of utility methods to do Queue Usage

Description

The list of methods to do Queue Usage are organized into topic(s).

Method

ListloadBalancingLeftHeavy(List tasks, int size)
load Balancing Left Heavy
Queue<T> sucQueue = new LinkedList<T>();
sucQueue.addAll(tasks);
int todo_size = sucQueue.size();
List<List<T>> pList = new ArrayList<List<T>>(size);
for (int i = 0; i < size; i++)
    pList.add(new LinkedList<T>());
T t1;
int b = 0;
...
voidofferUntilSuccess(T entry, Queue queue)
Certain types of queues will fail to java.util.Queue#offer(Object) an item due to many factors depending on the type of queue.
boolean success;
do {
    success = queue.offer(entry);
    Thread.yield();
} while (!success);
QueueparsePathToStack(String path)
parse Path To Stack
Queue<String> queue = new LinkedList<>();
String[] pathParts = path.split("/");
if (pathParts != null && pathParts.length > 0) {
    for (String part : pathParts) {
        if (part.length() > 0) {
            queue.add(part);
return queue;
TpollWhileNotEmpty(Queue queue)
Certain types of queues will return null when calling java.util.Queue#poll() due to many factors depending on the type of queue.
T item = queue.poll();
while (!queue.isEmpty() && item == null) {
    Thread.yield();
    item = queue.poll();
return item;
voidqueue(final Queue queue, final Object o, final Class clazz, final boolean removeNull)
Cast the object and feel the queue.
if (o != null && Queue.class.isAssignableFrom(o.getClass())) {
    Queue<?> mObj = (Queue<?>) o;
    for (Object obj : mObj) {
        setQueueValue(queue, obj, clazz, removeNull);
char[]randomizeIndex(char[] array)
randomize Index
char[] randomArray = new char[array.length];
List<Integer> set = new ArrayList<>();
while (set.size() < array.length) {
    int index = (int) (Math.random() * array.length);
    if (!set.contains(index)) {
        set.add(index);
Queue<Integer> queue = new LinkedList<>(set);
int i = 0;
while (!queue.isEmpty()) {
    randomArray[i++] = array[queue.poll()];
return randomArray;
Stringscramble(Object object)
Will alter the order of the letters in the String representation of an Object.
String string = object.toString();
char[] array = string.toCharArray();
char[] randomArray = randomizeIndex(array);
return new String(randomArray);
voidsetQueueValue(final Queue queue, final Object obj, final Class clazz, final boolean removeNull)
Set the queue value.
if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
    queue.add(clazz.cast(obj));
} else if (obj == null && !removeNull) {
    queue.add(null);
voidtoArrayTest(Queue q)
to Array Test
if (q.toArray().length != 0) {
    throw new RuntimeException();
Object testObject = new Object();
q.add(testObject);
Object[] result = q.toArray();
verify(result.length == 1);
verify(result[0] == testObject);
...
voidtransferAllElementsFromListToQueue(final List elements, Queue queue)
transfer All Elements From List To Queue
for (final Type element : elements) {
    queue.add(element);