本文整理汇总了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();
}
}
示例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);
}
示例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;
}
示例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());
}
}
示例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();
}
}
示例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();
}
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}