APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.utils

public Class Queue<T>

extends Object
implements Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Imports
com.badlogic.gdx.utils.reflect.ArrayReflection, java.util.Arrays, .Iterator, .NoSuchElementException

A resizable, ordered array of objects with efficient add and remove at the beginning and end. Values in the backing array may wrap back to the beginning, making add and remove at the beginning and end O(1) (unless the backing array needs to resize when adding). Deque functionality is provided via removeLast() and addFirst(Object).

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class

Field Summary

Modifier and TypeField and Description
protected int
head

Index of first element.

private transient Queue.QueueIterable<T>
public int
size

Number of elements in the queue.

protected int
tail

Index of last element.

protected T[]
values

Contains the values in the queue.

Constructor Summary

AccessConstructor and Description
public
Queue()

Creates a new Queue which can hold 16 values without needing to resize backing array.

public
Queue(int initialSize)

Creates a new Queue which can hold the specified number of values without needing to resize backing array.

public
Queue(int initialSize, Class<T> type)
Deprecated Use Queue#Queue(int, ArraySupplier) instead

Creates a new Queue which can hold the specified number of values without needing to resize backing array.

public
Queue(int initialSize, ArraySupplier<T[]> arraySupplier)

Creates a new Queue which can hold the specified number of values without needing to resize backing array.

Method Summary

Modifier and TypeMethod and Description
public void
addFirst(T
can be null
object
)

Prepend given object to the head.

public void
addLast(T
can be null
object
)

Append given object to the tail.

public void
clear()

Removes all values from this queue.

public void
ensureCapacity(int additional)

Increases the size of the backing array to accommodate the specified number of additional items.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public boolean
equalsIdentity(Object o)

Uses == for comparison of each item.

public T
first()

Returns the first (head) item in the queue (without removing it).

public T
get(int index)

Retrieves the value in queue without removing it.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public int

Returns:

An index of first occurrence of value in queue or -1 if no such value exists
indexOf
(T value, boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns the index of first occurrence of value in the queue, or -1 if no such value exists.

public boolean
isEmpty()

Returns true if the queue is empty.

public Iterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator for the items in the queue.

public T
last()

Returns the last (tail) item in the queue (without removing it).

public boolean
notEmpty()

Returns true if the queue has one or more items.

public T

Returns:

removed object
removeFirst
()

Remove the first item from the queue.

public T
removeIndex(int index)

Removes and returns the item at the specified index.

public T

Returns:

removed object
removeLast
()

Remove the last item from the queue.

public boolean

Returns:

true if value was found and removed, false otherwise
removeValue
(T value, boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Removes the first instance of the specified value in the queue.

protected void
resize(int newSize)

Resize backing array.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
toString(String separator)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

iterableback to summary
private transient Queue.QueueIterable<T> iterable
sizeback to summary
public int size

Number of elements in the queue.

tailback to summary
protected int tail

Index of last element. Logically bigger than head. Usually points to an empty position, but points to the head when full (size == values.length).

valuesback to summary
protected T[] values

Contains the values in the queue. Head and tail indices go in a circle around this array, wrapping at the end.

Constructor Detail

Queueback to summary
public Queue()

Creates a new Queue which can hold 16 values without needing to resize backing array.

Queueback to summary
public Queue(int initialSize)

Creates a new Queue which can hold the specified number of values without needing to resize backing array.

Queueback to summary
public Queue(int initialSize, Class<T> type)

Deprecated

Use Queue#Queue(int, ArraySupplier) instead

Creates a new Queue which can hold the specified number of values without needing to resize backing array. This creates backing array of the specified type via reflection, which is necessary only when accessing the backing array directly.

Annotations
@Deprecated
Queueback to summary
public Queue(int initialSize, ArraySupplier<T[]> arraySupplier)

Creates a new Queue which can hold the specified number of values without needing to resize backing array. This creates backing array of the specified type via the supplier, which is necessary only when accessing the backing array directly.

Method Detail

addFirstback to summary
public void addFirst(T object)

Prepend given object to the head. (enqueue to head) Unless backing array needs resizing, operates in O(1) time.

Parameters
object:T

can be null

See Also
addLast(Object)
addLastback to summary
public void addLast(T object)

Append given object to the tail. (enqueue to tail) Unless backing array needs resizing, operates in O(1) time.

Parameters
object:T

can be null

clearback to summary
public void clear()

Removes all values from this queue. Values in backing array are set to null to prevent memory leak, so this operates in O(n).

ensureCapacityback to summary
public void ensureCapacity(int additional)

Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

equalsIdentityback to summary
public boolean equalsIdentity(Object o)

Uses == for comparison of each item.

firstback to summary
public T first()

Returns the first (head) item in the queue (without removing it).

Exceptions
NoSuchElementException:
when queue is empty
See Also
addFirst(Object), removeFirst()
getback to summary
public T get(int index)

Retrieves the value in queue without removing it. Indexing is from the front to back, zero based. Therefore get(0) is the same as first().

Exceptions
IndexOutOfBoundsException:
when the index is negative or >= size
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
indexOfback to summary
public int indexOf(T value, boolean identity)

Returns the index of first occurrence of value in the queue, or -1 if no such value exists.

Parameters
identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

Returns:int

An index of first occurrence of value in queue or -1 if no such value exists

isEmptyback to summary
public boolean isEmpty()

Returns true if the queue is empty.

iteratorback to summary
public Iterator<T> iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator for the items in the queue. Remove is supported.

If Collections#allocateIterators is false, the same iterator instance is returned each time this method is called. Use the QueueIterator constructor for nested or multithreaded iteration.

lastback to summary
public T last()

Returns the last (tail) item in the queue (without removing it).

Exceptions
NoSuchElementException:
when queue is empty
See Also
addLast(Object), removeLast()
notEmptyback to summary
public boolean notEmpty()

Returns true if the queue has one or more items.

removeFirstback to summary
public T removeFirst()

Remove the first item from the queue. (dequeue from head) Always O(1).

Returns:T

removed object

Exceptions
NoSuchElementException:
when queue is empty
removeIndexback to summary
public T removeIndex(int index)

Removes and returns the item at the specified index.

removeLastback to summary
public T removeLast()

Remove the last item from the queue. (dequeue from tail) Always O(1).

Returns:T

removed object

Exceptions
NoSuchElementException:
when queue is empty
See Also
removeFirst()
removeValueback to summary
public boolean removeValue(T value, boolean identity)

Removes the first instance of the specified value in the queue.

Parameters
identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

Returns:boolean

true if value was found and removed, false otherwise

resizeback to summary
protected void resize(int newSize)

Resize backing array. newSize must be bigger than current size.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

toStringback to summary
public String toString(String separator)
com.badlogic.gdx.utils back to summary

public Class Queue.QueueIterable<T>

extends Object
implements Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable

Field Summary

Modifier and TypeField and Description
private final boolean
private Queue.QueueIterator<T>
private Queue.QueueIterator<T>
private final Queue<T>

Constructor Summary

AccessConstructor and Description
public
QueueIterable(Queue<T> queue)

public
QueueIterable(Queue<T> queue, boolean allowRemove)

Method Summary

Modifier and TypeMethod and Description
public Iterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowRemoveback to summary
private final boolean allowRemove
iterator1back to summary
private Queue.QueueIterator<T> iterator1
iterator2back to summary
private Queue.QueueIterator<T> iterator2
queueback to summary
private final Queue<T> queue

Constructor Detail

QueueIterableback to summary
public QueueIterable(Queue<T> queue)
QueueIterableback to summary
public QueueIterable(Queue<T> queue, boolean allowRemove)

Method Detail

iteratorback to summary
public Iterator<T> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

See Also
Collections#allocateIterators
com.badlogic.gdx.utils back to summary

public Class Queue.QueueIterator<T>

extends Object
implements Iterator<T>, Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, java.util.Iterator

Field Summary

Modifier and TypeField and Description
private final boolean
pack-priv int
private final Queue<T>
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public
QueueIterator(Queue<T> queue)

public
QueueIterator(Queue<T> queue, boolean allowRemove)

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public Iterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

public T
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowRemoveback to summary
private final boolean allowRemove
indexback to summary
pack-priv int index
queueback to summary
private final Queue<T> queue
validback to summary
pack-priv boolean valid

Constructor Detail

QueueIteratorback to summary
public QueueIterator(Queue<T> queue)
QueueIteratorback to summary
public QueueIterator(Queue<T> queue, boolean allowRemove)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

iteratorback to summary
public Iterator<T> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

nextback to summary
public T next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

resetback to summary
public void reset()