本文整理汇总了Java中java.util.Queue.remove方法的典型用法代码示例。如果您正苦于以下问题:Java Queue.remove方法的具体用法?Java Queue.remove怎么用?Java Queue.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Queue
的用法示例。
在下文中一共展示了Queue.remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: floodFill
import java.util.Queue; //导入方法依赖的package包/类
private void floodFill(int u, int v, int label) {
Queue<Point> Q = new LinkedList<Point>(); //queue contains pixel coordinates
Q.add(new Point(u, v));
while (!Q.isEmpty()) {
Point p = Q.remove(); // get the next point to process
int x = p.x;
int y = p.y;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)
&& getLabel(x, y) == FOREGROUND) {
setLabel(x, y, label);
Q.add(new Point(x+1, y));
Q.add(new Point(x, y+1));
Q.add(new Point(x, y-1));
Q.add(new Point(x-1, y));
}
}
}
示例2: visitIndirectNeighbours
import java.util.Queue; //导入方法依赖的package包/类
/**
* Applies the given {@link Consumer} to the identifiers of the indirect neighbours in the given direction.
*
* @param id the node's identifier
* @param direction the direction of neighbours to visit
* @param visited a function that returns true if the node with the supplied id has been visited during this
* iteration
* @param action the function to apply to each neighbour's identifier
*/
public void visitIndirectNeighbours(final int id, final SequenceDirection direction,
final Predicate<Integer> visited, final Consumer<Integer> action) {
final Queue<Integer> queue = new LinkedList<>();
queue.add(id);
while (!queue.isEmpty()) {
final int head = queue.remove();
if (visited.test(head)) {
continue;
}
action.accept(head);
visitDirectNeighbours(head, direction, index -> {
if (!visited.test(index)) {
queue.add(index);
}
});
}
}
示例3: galtonWatsonTree
import java.util.Queue; //导入方法依赖的package包/类
static <Node extends BinaryTreeNode<Node>> void galtonWatsonTree(BinaryTree<Node> t, int n) {
Random r = new Random();
Queue<Node> q = new LinkedList<Node>();
t.clear();
t.r = t.newNode();
q.add(t.r);
double p = ((double)0.5 - ((double)1)/(n+n));
while (!q.isEmpty()) {
Node u = q.remove();
if (r.nextDouble() < p) {
u.left = t.newNode();
u.left.parent = u;
q.add(u.left);
} if (r.nextDouble() < p) {
u.right = t.newNode();
u.right.parent = u;
q.add(u.right);
}
}
}
示例4: breadthFirstFrom
import java.util.Queue; //导入方法依赖的package包/类
public void breadthFirstFrom(final String start, final Walken christopher) {
final Queue<BuildRule> buildRules = new LinkedList<>();
final BuildRule startRule = map.get(start);
int depth = 0;
buildRules.add(startRule);
christopher.step(startRule, depth);
while (!buildRules.isEmpty()) {
final BuildRule parent = buildRules.remove();
depth++;
final Set<BuildRule> children = graph.predecessors(parent);
for (final BuildRule child : children) {
christopher.step(child, depth);
buildRules.add(child);
}
}
}
示例5: browseForFileObjects
import java.util.Queue; //导入方法依赖的package包/类
private Set<FileObject> browseForFileObjects(javax.swing.tree.TreeModel model) {
Queue<CheckNode> q = new LinkedList<CheckNode>();
q.add((CheckNode) model.getRoot());
Set<FileObject> result = new HashSet<FileObject>();
while (!q.isEmpty()) {
CheckNode node = q.remove();
Object uo = node.getUserObject();
if (uo instanceof FileTreeElement) {
FileTreeElement fileTreeElement = (FileTreeElement) uo;
Object userObject = fileTreeElement.getUserObject();
if (userObject instanceof FileObject) {
result.add((FileObject) userObject);
} else {
throw new IllegalArgumentException("Object of type FileObject was expected, but got " + userObject.getClass().getName());
}
}
for (int i = 0; i < model.getChildCount(node); i++) {
q.add((CheckNode) model.getChild(node, i));
}
}
return result;
}
示例6: reachableNodes
import java.util.Queue; //导入方法依赖的package包/类
/**
* Returns the set of nodes that are reachable from {@code node}. Node B is defined as reachable
* from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A
* and ending at node B. Note that a node is always reachable from itself via a zero-length path.
*
* <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view
* of the set of nodes reachable from {@code node}. In other words, the returned {@link Set} will
* not be updated after modifications to {@code graph}.
*
* @throws IllegalArgumentException if {@code node} is not present in {@code graph}
*/
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node);
Set<N> visitedNodes = new LinkedHashSet<N>();
Queue<N> queuedNodes = new ArrayDeque<N>();
visitedNodes.add(node);
queuedNodes.add(node);
// Perform a breadth-first traversal rooted at the input node.
while (!queuedNodes.isEmpty()) {
N currentNode = queuedNodes.remove();
for (N successor : graph.successors(currentNode)) {
if (visitedNodes.add(successor)) {
queuedNodes.add(successor);
}
}
}
return Collections.unmodifiableSet(visitedNodes);
}
示例7: leftistDraw
import java.util.Queue; //导入方法依赖的package包/类
/**
* Draw each node so that it's x-coordinate is as small
* as possible without intersecting any other node at the same level
* the same as its parent's
*/
public void leftistDraw() {
assignLevels();
Queue<GeometricTreeNode> queue = new LinkedList<GeometricTreeNode>();
//get root node
GeometricTreeNode p = r;
//set root position to 0
p.position.x = 0;
//add root node to queue
queue.add(p);
//loop through tree in breadth first traverse
while (!queue.isEmpty()) {
//get current node from queue
GeometricTreeNode c = queue.remove();
//if current node is same y, then x must be ++
c.position.x = (p.position.y == c.position.y) ? p.position.x + 1 : 0;
//ensure root is at .x = 0
if (c == r) {
c.position.x = 0;
}
//set parent node to current node
p = c;
//add child nodes if not null
//left then right to follow bfs
if (c.left != nil) {
queue.add(c.left);
}
if (c.right != nil) {
queue.add(c.right);
}
}
}
示例8: getMaxChildrenWidth
import java.util.Queue; //导入方法依赖的package包/类
/**
* Returns the maximum number of children with the same distance to the given node.
*
* @param node a {@link LayoutableNode}
* @return the maximum number of children with the same distance to the given node
*/
private int getMaxChildrenWidth(final LayoutableNode node) {
int[] maxChildrenWidth = {-1};
final Set<LayoutableNode> visited = new HashSet<>();
final Queue<LayoutableNode> queue = new LinkedList<>();
queue.add(node);
final int[] depthIncreaseTimes = {1, 0};
while (!queue.isEmpty()) {
final LayoutableNode head = queue.remove();
if (!visited.contains(head)) {
visited.add(head);
head.getOutgoingEdges().forEach(edge -> {
if (!visited.contains(edge.getTo())) {
depthIncreaseTimes[1]++;
queue.add(edge.getTo());
}
});
}
depthIncreaseTimes[0]--;
if (depthIncreaseTimes[0] <= 0) {
depthIncreaseTimes[0] = depthIncreaseTimes[1];
depthIncreaseTimes[1] = 0;
maxChildrenWidth[0] = Math.max(maxChildrenWidth[0], depthIncreaseTimes[0]);
if (depthIncreaseTimes[0] == 1) {
return maxChildrenWidth[0];
}
}
}
return maxChildrenWidth[0];
}
示例9: testFailureDetection
import java.util.Queue; //导入方法依赖的package包/类
@Test
public void testFailureDetection() throws Exception {
Time mockTime = getMockTime();
Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
try {
// Start detection.
detector.startDetection();
assertTrue(anomalies.isEmpty());
int brokerId = 0;
killBroker(brokerId);
long start = System.currentTimeMillis();
while (anomalies.isEmpty() && System.currentTimeMillis() < start + 30000) {
// wait for the anomalies to be drained.
}
assertEquals("One broker failure should have been detected before timeout.", 1, anomalies.size());
Anomaly anomaly = anomalies.remove();
assertTrue("The anomaly should be BrokerFailure", anomaly instanceof BrokerFailures);
BrokerFailures brokerFailures = (BrokerFailures) anomaly;
assertEquals("The failed broker should be 0 and time should be 100L", Collections.singletonMap(brokerId, 100L),
brokerFailures.failedBrokers());
// Bring the broker back
System.out.println("Starting brokers.");
restartDeadBroker(brokerId);
detector.detectBrokerFailures();
assertTrue(detector.failedBrokers().isEmpty());
} finally {
detector.shutdown();
}
}
示例10: calculateUptime
import java.util.Queue; //导入方法依赖的package包/类
/**
* Calculates the up time.
*
* @param difference the difference
* @param calculations the calculations
* @param labels the labels
* @return the uptime as a string.
*/
protected String calculateUptime(final double difference, final Queue<Integer> calculations, final Queue<String> labels) {
if (calculations.isEmpty()) {
return "";
}
final int value = calculations.remove();
final double time = Math.floor(difference / value);
final double newDifference = difference - time * value;
final String currentLabel = labels.remove();
final String label = time == 0 || time > 1 ? currentLabel + 's' : currentLabel;
return Integer.toString((int) time) + ' ' + label + ' ' + calculateUptime(newDifference, calculations, labels);
}
示例11: levelOrderQueue
import java.util.Queue; //导入方法依赖的package包/类
public void levelOrderQueue(Node root) {
Queue<Node> q = new LinkedList<Node>();
if (root == null)
return;
q.add(root);
while (!q.isEmpty()) {
Node n = (Node) q.remove();
int goal=11;
int c=0;
if (n.left != null)
q.add(n.left);
c+=1;
if (n.right != null)
q.add(n.right);
c+=1;
System.out.print(" " + n.data);
if(n.data==goal)
break;
}
}
示例12: readValue
import java.util.Queue; //导入方法依赖的package包/类
private double readValue(Queue<Token> tokens) throws Exception {
double sign = 1.0;
if (NEGATIVE.equals(tokens.peek())) {
sign = -1.0;
tokens.remove();
}
final Token token = tokens.remove();
double value = 0.0;
if (token instanceof NumberToken) {
final NumberToken number = (NumberToken) token;
value = number.value;
} else if (token instanceof NameToken) {
final NameToken name = (NameToken) token;
final Variable variable = variables.get(name.value);
if (variable == null) {
throw new Exception(String.format(
"Attempting to read value of \"%s\" before assigning value.",
name.value));
}
value = variable.read();
} else {
throw new Exception(String.format("Cannot evaluate %s", token));
}
return sign * value;
}
示例13: addImpliedFeatures
import java.util.Queue; //导入方法依赖的package包/类
/**
* Given a set of features, add to it all the features directly or indirectly
* implied by any of them, and return it.
* @param features the set of features to expand
* @return the same set of features, expanded with all implied features
*/
public static Set<Feature<?>> addImpliedFeatures(Set<Feature<?>> features) {
Queue<Feature<?>> queue = new ArrayDeque<Feature<?>>(features);
while (!queue.isEmpty()) {
Feature<?> feature = queue.remove();
for (Feature<?> implied : feature.getImpliedFeatures()) {
if (features.add(implied)) {
queue.add(implied);
}
}
}
return features;
}
示例14: bfTraverse
import java.util.Queue; //导入方法依赖的package包/类
public void bfTraverse() {
Queue<Node> q = new LinkedList<Node>();
q.add(r);
while (!q.isEmpty()) {
Node u = q.remove();
if (u.left != nil) q.add(u.left);
if (u.right != nil) q.add(u.right);
}
}
示例15: obtainLast
import java.util.Queue; //导入方法依赖的package包/类
private Event obtainLast(EventsQueue eventsQueue) {
Event lastEvent = null;
Queue<Event> queue = eventsQueue.queue.obtainQueue();
int count = queue.size();
for (int i = 0; i < count; i++) {
lastEvent = queue.remove();
}
return lastEvent;
}