Java 8 PriorityQueue - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 PriorityQueue class.

This code example shows how to use the following methods:offer, remove
    final E entry = super.get(key);
    if (entry != null) {
      if (accessExpirationPeriod > 0) {
        // update expiration time 
        final Entry existing = (Entry)entry;
        Preconditions.checkState(queue.remove(existing));
        setExpirationTime(existing, accessExpirationPeriod);
        queue.offer(existing);
      } 
    } 
    return entry;
  } 
 
  @Override 
  public E put(final E entry) {
    if (!(entry instanceof Entry)) {
      throw new HadoopIllegalArgumentException( 
          "!(entry instanceof Entry), entry.getClass()=" + entry.getClass());
    } 
 
7
CodeRank
This code example shows how to use the following methods:isEmpty, remove, add, peek, size
  public String getModuleName() {
    return "com.google.gwt.emultest.EmulSuite"; 
  } 
 
  public void testBasic() { 
    PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    assertEquals(0, pq.size());
    assertTrue(pq.isEmpty());
    assertNull(pq.peek());
    try { 
      pq.remove();
      fail("Expected exception"); 
    } catch (NoSuchElementException e) {
      // expected 
    } 
    pq.add(14);
    assertEquals(1, pq.size());
    assertFalse(pq.isEmpty());
    assertEquals(14, pq.peek().intValue());
    pq.add(5);

7
CodeRank
This code example shows how to use the following methods:add, poll, size
    /** 
     * update the single sample multiplicities by adding the current single sample multiplicity to the priority queue, and 
     * reset the current single sample multiplicity to 0. 
     */ 
    public void flushSingleSampleMultiplicity() { 
        singleSampleMultiplicities.add(currentSingleSampleMultiplicity);
        if( singleSampleMultiplicities.size() == singleSampleCapacity + 1 ) {
            singleSampleMultiplicities.poll(); // remove the lowest multiplicity from the list
        } else if( singleSampleMultiplicities.size() > singleSampleCapacity + 1 ) {
            throw new IllegalStateException("Somehow the per sample multiplicity list has grown too big: " + singleSampleMultiplicities);
        } 
        currentSingleSampleMultiplicity = 0;
    } 
 
    @Override 
    public void incMultiplicity(final int incr) {
        super.incMultiplicity(incr);
        currentSingleSampleMultiplicity += incr;
    } 
 
6
CodeRank
This code example shows how to use the following methods:isEmpty, remove, add, peek, size
  public String getModuleName() {
    return "com.google.gwt.emultest.EmulSuite"; 
  } 
 
  public void testBasic() { 
    PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    assertEquals(0, pq.size());
    assertTrue(pq.isEmpty());
    assertNull(pq.peek());
    try { 
      pq.remove();
      fail("Expected exception"); 
    } catch (NoSuchElementException e) {
      // expected 
    } 
    pq.add(14);
    assertEquals(1, pq.size());
    assertFalse(pq.isEmpty());
    assertEquals(14, pq.peek().intValue());
    pq.add(5);

7
CodeRank
This code example shows how to use the following methods:add, contains, size
        try { 
            if (contains(processId)) {
                return false; 
            } 
 
            int size = queue.size();
            queue.add(processId);
            if (size == 0) {
                notEmpty.signalAll();
            } 
            return true; 
        } finally { 
            lock.unlock();
        } 
    } 
 
    public boolean contains(Long processId) {
        return queue.contains(processId) || history.containsKey(processId);
    } 
 

7
CodeRank
This code example shows how to use the following methods:offer, peek, poll
public class Primary { 
	public static void main(String[] args) {
		 
		/*QUEUES are FIFO (usually), STACKS are LIFO (always)*/ 
		/*Create Queue*/ 
		PriorityQueue<String> q = new PriorityQueue<String>(); // Create OBJECT "q".
		 
		/*Now we need to populate our QUEUE with the BI METHOD "offer".*/ 
		q.offer("first"); // The first thing we pass in will be the highest priority.
		q.offer("second");
		q.offer("third");
		 
		System.out.printf("%s ", q); // print the ENTIRE QUEUE
		System.out.println();// blank line
		 
		/*Now we can do something called PEEKING. This is a way of looking at the element in your QUEUE that has the  
		 * highest PRIORITY, without CHANGING your QUEUE, or the LINEUP at all.*/ 
		 
		System.out.printf("%s ", q.peek()); //we use the "peek()" METHOD to PEEK at the QUEUE "q". Returns "first".
		System.out.println();//blank line

6
CodeRank
This code example shows how to use the following methods:add, contains, size
        try { 
            if (contains(processId)) {
                return false; 
            } 
 
            int size = tables.size();
            // tables.addLast(processId); 
            tables.add(processId);// 添加记录
            if (size == 0) {
                notEmpty.signalAll();
            } 
            return true; 
        } finally { 
            lock.unlock();
        } 
    } 
 
    public boolean remove(Long processId) {
        lock.lock();
        try { 

6
CodeRank
This code example shows how to use the following methods:
        queues.add(new ArrayBlockingQueue<Boolean>(count, false));
        queues.add(new ArrayBlockingQueue<Boolean>(count, true));
        queues.add(new LinkedBlockingQueue<Boolean>());
        queues.add(new LinkedBlockingDeque<Boolean>());
        queues.add(new PriorityBlockingQueue<Boolean>());
        queues.add(new PriorityQueue<Boolean>());
        queues.add(new LinkedList<Boolean>());
        queues.add(new LinkedTransferQueue<Boolean>());
 
        // Following additional implementations are available from: 
        // http://gee.cs.oswego.edu/dl/concurrency-interest/index.html 
        // queues.add(new SynchronizedLinkedListQueue<Boolean>()); 
 
        // Avoid "first fast, second slow" benchmark effect. 
        Collections.shuffle(queues);
        return queues;
    } 
 
    void prettyPrintResults() { 
        List<String> classNames = new ArrayList<String>(results.keySet());
6
CodeRank
This code example shows how to use the following methods:isEmpty, add, peek, poll, size
			} 
		}); 
	} 
	public int getMedian(int n)
	{ 
		if((maxHeap.size() + minHeap.size()) % 2 == 0)
		{ 
			if(!maxHeap.isEmpty() && n < maxHeap.peek())
			{ 
				maxHeap.add(n);
				n = maxHeap.poll();
			} 
			minHeap.add(n);
			return maxHeap.peek();
		} 
		else 
		{ 
			if(!minHeap.isEmpty() && n > minHeap.peek())
			{ 
				minHeap.add(n);

6
CodeRank
This code example shows how to use the following methods:add, peek, remove, size
        return 0; 
    } 
  }; 
 
  public void insert (T key, double value) {
    if (heap.size() < k) {
	    if (allowDuplicates)
        heap.add(new SimpleImmutableEntry<T,Double>(key, value));
	    else { 
        if (! contents.containsKey(key)) {
          heap.add(new SimpleImmutableEntry<T,Double>(key, value));
          contents.put(key,true);
        } 
	    } 
    } else if ((max && (heap.peek().getValue() < value)) ||
               ((!max) && (heap.peek().getValue() > value))) {
      SimpleImmutableEntry<T,Double> pair;
      pair = heap.peek();
	    if (allowDuplicates) {
        heap.remove(pair);

6
CodeRank
See Code Examples for Java 8 PriorityQueue Methods: