Queue
Code IndexAdd Codota to your IDE (free)

Best code snippets using java.util.Queue(Showing top 15 results out of 12,744)

  • Common ways to obtain Queue
private void myMethod () {
Queue q =
  • new SpscLinkedQueue<T>()
  • new ArrayDeque<Runnable>()
  • new SpscLinkedAtomicQueue<T>()
  • Smart code suggestions by Codota
}
origin: iluwatar/java-design-patterns

 /**
  * @param e number which we want to put to our queue
  */
 public synchronized void put(Integer e) {
  LOGGER.info("putting");
  sourceList.add(e);
  LOGGER.info("notifying");
  notify();
 }
}
origin: iluwatar/java-design-patterns

private void processPendingCommands() {
 Iterator<Runnable> iterator = pendingCommands.iterator();
 while (iterator.hasNext()) {
  Runnable command = iterator.next();
  command.run();
  iterator.remove();
 }
}
origin: iluwatar/java-design-patterns

void flush(SelectionKey key) throws IOException {
 Queue<Object> pendingWrites = channelToPendingWrites.get(key.channel());
 while (true) {
  Object pendingWrite = pendingWrites.poll();
  if (pendingWrite == null) {
   // We don't have anything more to write so channel is interested in reading more data
   reactor.changeOps(key, SelectionKey.OP_READ);
   break;
  }
  // ask the concrete channel to make sense of data and write it to java channel
  doWrite(pendingWrite, key);
 }
}
origin: google/guava

 @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_REMOVE})
 @CollectionSize.Require(SEVERAL)
 public void testPoll_sizeMany() {
  assertEquals("sizeManyQueue.poll() should return first element", e0(), getQueue().poll());
  expectMissing(e0());
 }
}
origin: google/guava

@Override
public E remove() {
 synchronized (mutex) {
  return delegate().remove();
 }
}
origin: google/guava

@Override
public boolean offer(E o) {
 assertTrue(Thread.holdsLock(mutex));
 return delegate.offer(o);
}
origin: google/guava

@CollectionSize.Require(ONE)
public void testElement_size1() {
 assertEquals("size1Queue.element() should return first element", e0(), getQueue().element());
 expectUnchanged();
}
origin: google/guava

public void testConsumingIterable_queue_removesFromQueue() {
 Queue<Integer> queue = Lists.newLinkedList(asList(5, 14));
 Iterator<Integer> consumingIterator = Iterables.consumingIterable(queue).iterator();
 assertEquals(5, queue.peek().intValue());
 assertEquals(5, consumingIterator.next().intValue());
 assertEquals(14, queue.peek().intValue());
 assertTrue(consumingIterator.hasNext());
 assertTrue(queue.isEmpty());
}
origin: google/guava

@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
public E poll() {
 return delegate().poll();
}
origin: google/guava

 @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_REMOVE})
 @CollectionSize.Require(SEVERAL)
 public void testRemove_sizeMany() {
  assertEquals("sizeManyQueue.remove() should return first element", e0(), getQueue().remove());
  expectMissing(e0());
 }
}
origin: google/guava

@Override
public boolean remove(Object object) {
 assertTrue(Thread.holdsLock(mutex));
 return delegate.remove(object);
}
origin: google/guava

@Override
public Object[] toArray() {
 assertTrue(Thread.holdsLock(mutex));
 return delegate.toArray();
}
origin: google/guava

@Override
public <T> T[] toArray(T[] array) {
 assertTrue(Thread.holdsLock(mutex));
 return delegate.toArray(array);
}
origin: google/guava

@Override
public E peek() {
 assertTrue(Thread.holdsLock(mutex));
 return delegate.peek();
}
origin: google/guava

@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(ONE)
public void testPoll_size1() {
 assertEquals("size1Queue.poll() should return first element", e0(), getQueue().poll());
 expectMissing(e0());
}
java.utilQueue

Javadoc

A collection designed for holding elements prior to processing. Besides basic java.util.Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

Throws exception Returns special value
Insert #add #offer
Remove #remove #poll
Examine #element #peek

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out). Whatever the ordering used, the head of the queue is that element which would be removed by a call to #remove() or #poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

The #offer method inserts an element if possible, otherwise returning false. This differs from the java.util.Collection#add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.

The #remove() and #poll() methods remove and return the head of the queue. Exactly which element is removed from the queue is a function of the queue's ordering policy, which differs from implementation to implementation. The remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null.

The #element() and #peek() methods return, but do not remove, the head of the queue.

The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the java.util.concurrent.BlockingQueue interface, which extends this interface.

Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

This interface is a member of the Java Collections Framework.

Most used methods

  • add
    Inserts the specified element into this queue if it is possible to do so immediately without violati
  • poll
    Retrieves and removes the head of this queue, or returns null if this queue is empty.
  • isEmpty
  • remove
  • size
  • offer
    Inserts the specified element into this queue if it is possible to do so immediately without violati
  • clear
  • peek
    Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
  • addAll
  • iterator
  • contains
  • toArray
  • contains,
  • toArray,
  • element,
  • stream,
  • forEach,
  • removeAll,
  • retainAll,
  • containsAll,
  • removeIf,
  • parallelStream

Popular classes and methods

  • getSystemService (Context)
  • setScale (BigDecimal)
    Returns a new BigDecimal instance with the specified scale. If the new scale is greater than the old
  • getSharedPreferences (Context)
  • BufferedImage (java.awt.image)
  • Kernel (java.awt.image)
  • RandomAccessFile (java.io)
    Saves binary data to the local storage; currently using hex encoding. The string is prefixed with "h
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • Collections (java.util)
    Collections contains static methods which operate on Collection classes.
  • Set (java.util)
    A Set is a data structure which does not allow duplicate elements.
  • Servlet (javax.servlet)
    Defines methods that all servlets must implement.A servlet is a small Java program that runs within

For IntelliJ IDEA,
Android Studio or Eclipse

  • Codota IntelliJ IDEA pluginCodota Android Studio pluginCode IndexSign in
  • EnterpriseFAQAboutContact Us
  • Terms of usePrivacy policyCodeboxFind Usages
Add Codota to your IDE (free)