当前位置: 首页>>代码示例>>Java>>正文


Java PriorityQueue.peek方法代码示例

本文整理汇总了Java中java.util.PriorityQueue.peek方法的典型用法代码示例。如果您正苦于以下问题:Java PriorityQueue.peek方法的具体用法?Java PriorityQueue.peek怎么用?Java PriorityQueue.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.peek方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onEventTime

import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
 * Occurs when an event-time timer fires due to watermark progression.
 *
 * @param timer the timer details.
 */
@Override
public void onEventTime(InternalTimer<K, VoidNamespace> timer) throws Exception {

    long currentWatermark = internalTimerService.currentWatermark();

    PriorityQueue<Long> sortedTimestamps = getSortedTimestamps();
    while (!sortedTimestamps.isEmpty() && sortedTimestamps.peek() <= currentWatermark) {
        long timestamp = sortedTimestamps.poll();
        for (T event : elementQueueState.get(timestamp)) {
            output.collect(new StreamRecord<>(event, timestamp));
        }
        elementQueueState.remove(timestamp);
    }

    if (sortedTimestamps.isEmpty()) {
        elementQueueState.clear();
    }

    if (!sortedTimestamps.isEmpty()) {
        saveRegisterWatermarkTimer();
    }
}
 
开发者ID:pravega,项目名称:flink-connectors,代码行数:28,代码来源:EventTimeOrderingOperator.java

示例2: getTopN

import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
 * Get top N elements
 *
 * @param vec the vec to extract the top elements from
 * @param N the number of elements to extract
 * @return the indices and the sorted top N elements
 */
private List<Double> getTopN(INDArray vec, int N) {
  BasicModelUtils.ArrayComparator comparator = new BasicModelUtils.ArrayComparator();
  PriorityQueue<Double[]> queue = new PriorityQueue<>(vec.rows(), comparator);

  for (int j = 0; j < vec.length(); j++) {
    final Double[] pair = new Double[] {vec.getDouble(j), (double) j};
    if (queue.size() < N) {
      queue.add(pair);
    } else {
      Double[] head = queue.peek();
      if (comparator.compare(pair, head) > 0) {
        queue.poll();
        queue.add(pair);
      }
    }
  }

  List<Double> lowToHighSimLst = new ArrayList<>();

  while (!queue.isEmpty()) {
    double ind = queue.poll()[1];
    lowToHighSimLst.add(ind);
  }
  return Lists.reverse(lowToHighSimLst);
}
 
开发者ID:tteofili,项目名称:par2hier,代码行数:33,代码来源:Par2Hier.java

示例3: getMostUnderservedQueues

import java.util.PriorityQueue; //导入方法依赖的package包/类
protected Collection<TempQueue> getMostUnderservedQueues(
    PriorityQueue<TempQueue> orderedByNeed, TQComparator tqComparator) {
  ArrayList<TempQueue> underserved = new ArrayList<TempQueue>();
  while (!orderedByNeed.isEmpty()) {
    TempQueue q1 = orderedByNeed.remove();
    underserved.add(q1);
    TempQueue q2 = orderedByNeed.peek();
    // q1's pct of guaranteed won't be larger than q2's. If it's less, then
    // return what has already been collected. Otherwise, q1's pct of
    // guaranteed == that of q2, so add q2 to underserved list during the
    // next pass.
    if (q2 == null || tqComparator.compare(q1,q2) < 0) {
      return underserved;
    }
  }
  return underserved;
}
 
开发者ID:naver,项目名称:hadoop,代码行数:18,代码来源:ProportionalCapacityPreemptionPolicy.java

示例4: test1

import java.util.PriorityQueue; //导入方法依赖的package包/类
@Test public void test1() {
  int[] a = new int[]{1000,900,8,7,3,2,1,90000};
  int n=5;
  PriorityQueue<Integer> q = new PriorityQueue<>(n, new Comparator<Integer>() {
    @Override public int compare(Integer o1, Integer o2) {
      return o2-o1;
    }
  });

  for(int i: a) {
    if( q.size() < n ) q.add(i);
    else {
      if( i < q.peek() ) {
        q.poll();
        q.add(i);
      }
    }
  }

  while(!q.isEmpty()) {
    System.out.println(q.poll());
  }
}
 
开发者ID:spennihana,项目名称:FasterWordEmbeddings,代码行数:24,代码来源:TopNTest.java

示例5: onTimer

import java.util.PriorityQueue; //导入方法依赖的package包/类
@Override
public void onTimer(long timestamp, OnTimerContext context,
    Collector<Tuple3<String, Long, String>> out) throws Exception {
  PriorityQueue<Tuple3<String, Long, String>> queue = queueState.value();
  Long watermark = context.timerService().currentWatermark();

  Tuple3<String, Long, String> head = queue.peek();
  boolean emitAll = queue.size() > MAX_NUMBER_OF_QUEUED_ELEMENTS;

  while (head != null && (head.f1 <= watermark || emitAll)) {
    out.collect(head);
    queue.remove(head);
    head = queue.peek();
  }
}
 
开发者ID:ehabqadah,项目名称:in-situ-processing-datAcron,代码行数:16,代码来源:RawMessagesSorter.java

示例6: onTimer

import java.util.PriorityQueue; //导入方法依赖的package包/类
@Override
public void onTimer(long timestamp, OnTimerContext context, Collector<AisMessage> out)
    throws Exception {
  PriorityQueue<AisMessage> queue = queueState.value();
  Long watermark = context.timerService().currentWatermark();

  AisMessage head = queue.peek();
  boolean emitAll = queue.size() > MAX_NUMBER_OF_QUEUED_ELEMENTS;

  while (head != null && (head.timestamp <= watermark || emitAll)) {
    out.collect(head);
    queue.remove(head);
    head = queue.peek();
  }
}
 
开发者ID:ehabqadah,项目名称:in-situ-processing-datAcron,代码行数:16,代码来源:AisMessagesStreamSorter.java

示例7: main

import java.util.PriorityQueue; //导入方法依赖的package包/类
@SuppressWarnings("Duplicates")
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    PriorityQueue<Integer> minHeap = new PriorityQueue<>(10, Collections.reverseOrder());
    PriorityQueue<Integer> maxHeap = new PriorityQueue<>(10);

    int n = scanner.nextInt();

    for (int i = 0; i < n; i++) {
        int num = scanner.nextInt();

        if (minHeap.size() <= maxHeap.size()) {
            minHeap.add(num);
        } else {
            maxHeap.add(num);
        }

        while (!minHeap.isEmpty() && !maxHeap.isEmpty() && minHeap.peek() > maxHeap.peek()) {
            int min = minHeap.poll();
            int max = maxHeap.poll();

            minHeap.add(max);
            maxHeap.add(min);
        }

        double median = (minHeap.size() == maxHeap.size() ? ((minHeap.peek() + maxHeap.peek()) / 2.0) : minHeap.peek());

        System.out.println(median);
    }
}
 
开发者ID:viatsko,项目名称:hack,代码行数:32,代码来源:HeapsFindTheRunningMedian.java

示例8: clusterDijkstra

import java.util.PriorityQueue; //导入方法依赖的package包/类
protected BroadcastTree clusterDijkstra(Cluster c, DatapathId root,
                                 Map<Link, Integer> linkCost,
                                 boolean isDstRooted) {
	
    HashMap<DatapathId, Link> nexthoplinks = new HashMap<DatapathId, Link>();
    HashMap<DatapathId, Integer> cost = new HashMap<DatapathId, Integer>();
    int w;

    for (DatapathId node : c.links.keySet()) {
        nexthoplinks.put(node, null);
        cost.put(node, MAX_PATH_WEIGHT);
    }

    HashMap<DatapathId, Boolean> seen = new HashMap<DatapathId, Boolean>();
    PriorityQueue<NodeDist> nodeq = new PriorityQueue<NodeDist>();
    
    nodeq.add(new NodeDist(root, 0));
    cost.put(root, 0);
    
    while (nodeq.peek() != null) {
        NodeDist n = nodeq.poll();
        DatapathId cnode = n.getNode();
        
        int cdist = n.getDist();
        if (cdist >= MAX_PATH_WEIGHT) break;
        if (seen.containsKey(cnode)) continue;
        
        seen.put(cnode, true);

        for (Link link : c.links.get(cnode)) {
            DatapathId neighbor;

            if (isDstRooted == true) {
            	neighbor = link.getSrc();
            } else {
            	neighbor = link.getDst();
            }

            // links directed toward cnode will result in this condition
            if (neighbor.equals(cnode)) continue;

            if (seen.containsKey(neighbor)) continue;

            if (linkCost == null || linkCost.get(link) == null) {
            	w = 1;
            } else {
            	w = linkCost.get(link);
            }

            int ndist = cdist + w; // the weight of the link, always 1 in current version of floodlight.
            if (ndist < cost.get(neighbor)) {
                cost.put(neighbor, ndist);
                nexthoplinks.put(neighbor, link);
                
                NodeDist ndTemp = new NodeDist(neighbor, ndist);
                // Remove an object that's already in there.
                // Note that the comparison is based on only the node id,
                // and not node id and distance.
                nodeq.remove(ndTemp);
                // add the current object to the queue.
                nodeq.add(ndTemp);
            }
        }
    }

    BroadcastTree ret = new BroadcastTree(nexthoplinks, cost);
    return ret;
}
 
开发者ID:xuraylei,项目名称:fresco_floodlight,代码行数:69,代码来源:TopologyInstance.java

示例9: dijkstra

import java.util.PriorityQueue; //导入方法依赖的package包/类
protected BroadcastTree dijkstra(Map<DatapathId, Set<Link>> links, DatapathId root,
           Map<Link, Integer> linkCost,
           boolean isDstRooted) {
   	HashMap<DatapathId, Link> nexthoplinks = new HashMap<DatapathId, Link>();
   	HashMap<DatapathId, Integer> cost = new HashMap<DatapathId, Integer>();
   	int w;
   	
   	for (DatapathId node : links.keySet()) {
   		nexthoplinks.put(node, null);
   		cost.put(node, MAX_PATH_WEIGHT);
   	}
	
   	HashMap<DatapathId, Boolean> seen = new HashMap<DatapathId, Boolean>();
   	PriorityQueue<NodeDist> nodeq = new PriorityQueue<NodeDist>();
   	nodeq.add(new NodeDist(root, 0));
   	cost.put(root, 0);
   	
   	while (nodeq.peek() != null) {
   		NodeDist n = nodeq.poll();
   		DatapathId cnode = n.getNode();
   		int cdist = n.getDist();
   		
   		if (cdist >= MAX_PATH_WEIGHT) break;
   		if (seen.containsKey(cnode)) continue;
   		seen.put(cnode, true);

   		for (Link link : links.get(cnode)) {
   			DatapathId neighbor;

   			if (isDstRooted == true) {
   				neighbor = link.getSrc();
   			} else {
   				neighbor = link.getDst();
   			}
       
   			// links directed toward cnode will result in this condition
   			if (neighbor.equals(cnode)) continue;

   			if (seen.containsKey(neighbor)) continue;

   			if (linkCost == null || linkCost.get(link) == null) {
   				w = 1;
   			} else {
   				w = linkCost.get(link);
   			}
   			
   			int ndist = cdist + w; // the weight of the link, always 1 in current version of floodlight.
   			if (ndist < cost.get(neighbor)) {
   				cost.put(neighbor, ndist);
   				nexthoplinks.put(neighbor, link);
   				
   				NodeDist ndTemp = new NodeDist(neighbor, ndist);
   				// Remove an object that's already in there.
   				// Note that the comparison is based on only the node id,
   				// and not node id and distance.
   				nodeq.remove(ndTemp);
   				// add the current object to the queue.
   				nodeq.add(ndTemp);
   			}
   		}
   	}

   	BroadcastTree ret = new BroadcastTree(nexthoplinks, cost);

	return ret;
}
 
开发者ID:xuraylei,项目名称:fresco_floodlight,代码行数:67,代码来源:TopologyInstance.java

示例10: dijkstra

import java.util.PriorityQueue; //导入方法依赖的package包/类
protected BroadcastTree dijkstra(Cluster c, DatapathId root,
                                 Map<Link, Integer> linkCost,
                                 boolean isDstRooted) {
    HashMap<DatapathId, Link> nexthoplinks = new HashMap<DatapathId, Link>();
    //HashMap<Long, Long> nexthopnodes = new HashMap<Long, Long>();
    HashMap<DatapathId, Integer> cost = new HashMap<DatapathId, Integer>();
    int w;

    for (DatapathId node: c.links.keySet()) {
        nexthoplinks.put(node, null);
        //nexthopnodes.put(node, null);
        cost.put(node, MAX_PATH_WEIGHT);
    }

    HashMap<DatapathId, Boolean> seen = new HashMap<DatapathId, Boolean>();
    PriorityQueue<NodeDist> nodeq = new PriorityQueue<NodeDist>();
    nodeq.add(new NodeDist(root, 0));
    cost.put(root, 0);
    while (nodeq.peek() != null) {
        NodeDist n = nodeq.poll();
        DatapathId cnode = n.getNode();
        int cdist = n.getDist();
        if (cdist >= MAX_PATH_WEIGHT) break;
        if (seen.containsKey(cnode)) continue;
        seen.put(cnode, true);

        for (Link link: c.links.get(cnode)) {
            DatapathId neighbor;

            if (isDstRooted == true) neighbor = link.getSrc();
            else neighbor = link.getDst();

            // links directed toward cnode will result in this condition
            if (neighbor.equals(cnode)) continue;

            if (seen.containsKey(neighbor)) continue;

            if (linkCost == null || linkCost.get(link)==null) w = 1;
            else w = linkCost.get(link);

            int ndist = cdist + w; // the weight of the link, always 1 in current version of floodlight.
            if (ndist < cost.get(neighbor)) {
                cost.put(neighbor, ndist);
                nexthoplinks.put(neighbor, link);
                //nexthopnodes.put(neighbor, cnode);
                NodeDist ndTemp = new NodeDist(neighbor, ndist);
                // Remove an object that's already in there.
                // Note that the comparison is based on only the node id,
                // and not node id and distance.
                nodeq.remove(ndTemp);
                // add the current object to the queue.
                nodeq.add(ndTemp);
            }
        }
    }

    BroadcastTree ret = new BroadcastTree(nexthoplinks, cost);
    return ret;
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:60,代码来源:TopologyInstance.java


注:本文中的java.util.PriorityQueue.peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。