Tabnine Logo
PriorityQueue
Code IndexAdd Tabnine to your IDE (free)

How to use
PriorityQueue
in
java.util

Best Java code snippets using java.util.PriorityQueue (Showing top 20 results out of 7,560)

Refine searchRefine arrow

  • ReentrantLock
  • Delayed
  • Condition
origin: google/guava

/**
 * Creates an empty {@code PriorityQueue} with the ordering given by its elements' natural
 * ordering.
 *
 * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0).
 */
public static <E extends Comparable> PriorityQueue<E> newPriorityQueue() {
 return new PriorityQueue<E>();
}
origin: hankcs/HanLP

/**
 * 添加一个元素
 * @param e 元素
 * @return 是否添加成功
 */
public boolean add(E e)
{
  if (queue.size() < maxSize)
  { // 未达到最大容量,直接添加
    queue.add(e);
    return true;
  }
  else
  { // 队列已满
    E peek = queue.peek();
    if (queue.comparator().compare(e, peek) > 0)
    { // 将新元素与当前堆顶元素比较,保留较小的元素
      queue.poll();
      queue.add(e);
      return true;
    }
  }
  return false;
}
origin: hankcs/HanLP

/**
 * 转为有序列表,自毁性操作
 * @return
 */
public List<E> toList()
{
  ArrayList<E> list = new ArrayList<E>(queue.size());
  while (!queue.isEmpty())
  {
    list.add(0, queue.poll());
  }
  return list;
}
origin: neo4j/neo4j

private void reinsert( Node<E,P> node )
{
  queue.remove( node );
  queue.add( node );
}
origin: neo4j/neo4j

public void sort()
{
  count = heap.size();
  array = new Object[ count ];
  // We keep the values in reverse order so that we can write from start to end
  for ( int i = 0; i < count; i++ )
  {
    array[i] = heap.poll();
  }
}
origin: robolectric/robolectric

private boolean nextTaskIsScheduledBefore(long endingTime) {
 return !runnables.isEmpty() && runnables.peek().scheduledTime <= endingTime;
}
origin: robovm/robovm

lock.lockInterruptibly();
try {
  for (;;) {
    E first = q.peek();
    if (first == null)
      available.await();
    else {
      long delay = first.getDelay(NANOSECONDS);
      if (delay <= 0)
        return q.poll();
        available.await();
      else {
        Thread thisThread = Thread.currentThread();
        leader = thisThread;
        try {
          available.awaitNanos(delay);
        } finally {
          if (leader == thisThread)
  if (leader == null && q.peek() != null)
    available.signal();
  lock.unlock();
origin: robovm/robovm

/**
 * @throws UnsupportedOperationException {@inheritDoc}
 * @throws ClassCastException            {@inheritDoc}
 * @throws NullPointerException          {@inheritDoc}
 * @throws IllegalArgumentException      {@inheritDoc}
 */
public int drainTo(Collection<? super E> c) {
  if (c == null)
    throw new NullPointerException();
  if (c == this)
    throw new IllegalArgumentException();
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    int n = 0;
    for (E e; (e = peekExpired()) != null;) {
      c.add(e);       // In this order, in case add() throws.
      q.poll();
      ++n;
    }
    return n;
  } finally {
    lock.unlock();
  }
}
origin: robovm/robovm

/**
 * Retrieves and removes the head of this queue, or returns {@code null}
 * if this queue has no elements with an expired delay.
 *
 * @return the head of this queue, or {@code null} if this
 *         queue has no elements with an expired delay
 */
public E poll() {
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    E first = q.peek();
    if (first == null || first.getDelay(NANOSECONDS) > 0)
      return null;
    else
      return q.poll();
  } finally {
    lock.unlock();
  }
}
origin: robovm/robovm

/**
 * Inserts the specified element into this delay queue.
 *
 * @param e the element to add
 * @return {@code true}
 * @throws NullPointerException if the specified element is null
 */
public boolean offer(E e) {
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    q.offer(e);
    if (q.peek() == e) {
      leader = null;
      available.signal();
    }
    return true;
  } finally {
    lock.unlock();
  }
}
origin: prestodb/presto

public int size()
{
  lock.lock();
  try {
    int total = 0;
    for (PriorityQueue<PrioritizedSplitRunner> level : levelWaitingSplits) {
      total += level.size();
    }
    return total;
  }
  finally {
    lock.unlock();
  }
}
origin: prestodb/presto

public void remove(PrioritizedSplitRunner split)
{
  checkArgument(split != null, "split is null");
  lock.lock();
  try {
    for (PriorityQueue<PrioritizedSplitRunner> level : levelWaitingSplits) {
      level.remove(split);
    }
  }
  finally {
    lock.unlock();
  }
}
origin: robovm/robovm

/**
 * Saves this queue to a stream (that is, serializes it).
 *
 * For compatibility with previous version of this class, elements
 * are first copied to a java.util.PriorityQueue, which is then
 * serialized.
 */
private void writeObject(java.io.ObjectOutputStream s)
  throws java.io.IOException {
  lock.lock();
  try {
    // avoid zero capacity argument
    q = new PriorityQueue<E>(Math.max(size, 1), comparator);
    q.addAll(this);
    s.defaultWriteObject();
  } finally {
    q = null;
    lock.unlock();
  }
}
origin: prestodb/presto

lock.lock();
try {
  if (levelWaitingSplits.get(level).isEmpty()) {
  levelWaitingSplits.get(level).offer(split);
  notEmpty.signal();
  lock.unlock();
origin: apache/hbase

if(!evictionLock.tryLock()) return;
  PriorityQueue<BlockBucket> bucketQueue = new PriorityQueue<>(3);
  bucketQueue.add(bucketSingle);
  bucketQueue.add(bucketMulti);
  bucketQueue.add(bucketMemory);
  while ((bucket = bucketQueue.poll()) != null) {
   long overflow = bucket.overflow();
   if (overflow > 0) {
 stats.evict();
 evictionInProgress = false;
 evictionLock.unlock();
origin: robovm/robovm

/**
 * Retrieves, but does not remove, the head of this queue, or
 * returns {@code null} if this queue is empty.  Unlike
 * {@code poll}, if no expired elements are available in the queue,
 * this method returns the element that will expire next,
 * if one exists.
 *
 * @return the head of this queue, or {@code null} if this
 *         queue is empty
 */
public E peek() {
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    return q.peek();
  } finally {
    lock.unlock();
  }
}
origin: prestodb/presto

  private void testCustomAggregation(Double[] values, int n)
  {
    PriorityQueue<Double> heap = new PriorityQueue<>(n, (x, y) -> -Double.compare(x, y));
    Arrays.stream(values).filter(x -> x != null).forEach(heap::add);
    Double[] expected = new Double[heap.size()];
    for (int i = heap.size() - 1; i >= 0; i--) {
      expected[i] = heap.remove();
    }
    testAggregation(Arrays.asList(expected), createDoublesBlock(values), createLongRepeatBlock(n, values.length));
  }
}
origin: redisson/redisson

@Override
public void nextCallback(T value) {
  values.add(value);
}
origin: apache/incubator-druid

@Override
public TopNResultBuilder addEntry(DimensionAndMetricValueExtractor dimensionAndMetricValueExtractor)
{
 Object dimensionValueObj = dimensionAndMetricValueExtractor.getDimensionValue(dimSpec.getOutputName());
 String dimensionValue = Objects.toString(dimensionValueObj, null);
 if (shouldAdd(dimensionValue)) {
  pQueue.add(
    new DimValHolder.Builder().withDimValue(dimensionValue)
                 .withMetricValues(dimensionAndMetricValueExtractor.getBaseObject())
                 .build()
  );
  if (pQueue.size() > threshold) {
   pQueue.poll();
  }
 }
 return this;
}
origin: stanfordnlp/CoreNLP

private static int[][] countCounts2IntArrays(Counter<Integer> countCounts) {
 int size = countCounts.size();
 int[][] arrays = new int[2][];
 arrays[0] = new int[size]; // counts
 arrays[1] = new int[size]; // count counts
 PriorityQueue<Integer> q = new PriorityQueue<>(countCounts.keySet());
 int i = 0;
 while (!q.isEmpty()) {
  Integer count = q.poll();
  Integer countCount = (int) Math.round(countCounts.getCount(count));
  arrays[0][i] = count;
  arrays[1][i] = countCount;
  i++;
 }
 return arrays;
}
java.utilPriorityQueue

Javadoc

A PriorityQueue holds elements on a priority heap, which orders the elements according to their natural order or according to the comparator specified at construction time. If the queue uses natural ordering, only elements that are comparable are permitted to be inserted into the queue.

The least element of the specified ordering is the first retrieved with #poll() and the greatest element is the last.

A PriorityQueue is not synchronized. If multiple threads will have to access it concurrently, use the java.util.concurrent.PriorityBlockingQueue.

Most used methods

  • <init>
    Creates a PriorityQueue containing the elements in the specified sorted set. This priority queue wil
  • add
    Inserts the specified element into this priority queue.
  • poll
    Gets and removes the head of the queue.
  • isEmpty
  • peek
    Gets but does not remove the head of the queue.
  • size
    Gets the size of the priority queue. If the size of the queue is greater than the Integer.MAX, then
  • remove
    Removes a single instance of the specified element from this queue, if it is present. More formally,
  • offer
    Inserts the specified element into this priority queue.
  • clear
    Removes all of the elements from this priority queue. The queue will be empty after this call return
  • addAll
  • iterator
    Returns an iterator over the elements in this queue. The iterator does not return the elements in an
  • toArray
    Returns an array containing all of the elements in this queue; the runtime type of the returned arra
  • iterator,
  • toArray,
  • contains,
  • comparator,
  • element,
  • removeAll,
  • siftUp,
  • removeAt,
  • siftDown,
  • containsAll

Popular in Java

  • Creating JSON documents from java classes using gson
  • scheduleAtFixedRate (ScheduledExecutorService)
  • setContentView (Activity)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • BufferedImage (java.awt.image)
    The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All
  • InputStreamReader (java.io)
    A class for turning a byte stream into a character stream. Data read from the source input stream is
  • SocketException (java.net)
    This SocketException may be thrown during socket creation or setting options, and is the superclass
  • URL (java.net)
    A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC
  • Executors (java.util.concurrent)
    Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory,
  • ZipFile (java.util.zip)
    This class provides random read access to a zip file. You pay more to read the zip file's central di
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now