本文整理匯總了Java中java.util.Queue.poll方法的典型用法代碼示例。如果您正苦於以下問題:Java Queue.poll方法的具體用法?Java Queue.poll怎麽用?Java Queue.poll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Queue
的用法示例。
在下文中一共展示了Queue.poll方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.util.Queue; //導入方法依賴的package包/類
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int n = sc.nextInt();
int minPower = 0;
Queue<Integer> queue = new PriorityQueue<>();
for(int i=0; i<n; i++){
queue.add(sc.nextInt());
}
int first = 0, second = 0, temp = 0;
while (queue.size() > 1){
first = queue.poll();
second = queue.poll();
temp = first + second;
queue.add(temp);
minPower += temp;
}
System.out.println(minPower);
}
}
示例2: zigzagLevelOrder
import java.util.Queue; //導入方法依賴的package包/類
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) return new LinkedList<>();
List<List<Integer>> list = new LinkedList<>();
List<Integer> l = new LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int zigzag = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode t = q.poll();
l.add(t.val);
if (t.left != null) q.offer(t.left);
if (t.right != null) q.offer(t.right);
}
if (zigzag % 2 == 1) {
Collections.reverse(l);
}
zigzag++;
list.add(l);
System.out.println(l);
l = new LinkedList<>();
}
return list;
}
示例3: findScrollableViewInternal
import java.util.Queue; //導入方法依賴的package包/類
protected View findScrollableViewInternal(View content, boolean selfable) {
View scrollableView = null;
Queue<View> views = new LinkedBlockingQueue<>(Collections.singletonList(content));
while (!views.isEmpty() && scrollableView == null) {
View view = views.poll();
if (view != null) {
if ((selfable || view != content) && (view instanceof AbsListView
|| view instanceof ScrollView
|| view instanceof ScrollingView
|| view instanceof NestedScrollingChild
|| view instanceof NestedScrollingParent
|| view instanceof WebView
|| view instanceof ViewPager)) {
scrollableView = view;
} else if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int j = 0; j < group.getChildCount(); j++) {
views.add(group.getChildAt(j));
}
}
}
}
return scrollableView;
}
示例4: topologicalSort
import java.util.Queue; //導入方法依賴的package包/類
public Set<Vertex> topologicalSort(Vertex startNode,Set<Vertex> set){
Queue<Vertex> queue=new LinkedList<>();
queue.add(startNode);
while(!queue.isEmpty()){
Vertex u=queue.poll();
set.add(u);
for(Vertex v : u.adjacents)
{
if(!set.contains(v)){
v.inDegree--;
if(v.inDegree==0)
queue.add(v);
}
else
return new HashSet<Vertex>();
}
}
return set;
}
示例5: bfs
import java.util.Queue; //導入方法依賴的package包/類
public int[] bfs(int[][] matrix,int s){
int[] dist=new int[matrix.length];
for(int i=0;i<dist.length;i++)
dist[i]=-1;
dist[s]=0;
Queue<Integer> queue=new LinkedList<>();
HashSet<Integer> visited=new HashSet<>();
queue.add(s);
visited.add(s);
while(!queue.isEmpty()){
int v=queue.poll();
for(int i=0;i<matrix.length;i++){
if(matrix[v][i]==1 && !visited.contains(i)){
visited.add(i);
queue.add(i);
dist[i]=dist[v]+6;
}
}
}
return dist;
}
示例6: dispatch
import java.util.Queue; //導入方法依賴的package包/類
@Override
void dispatch(Object event, Iterator<Subscriber> subscribers) {
checkNotNull(event);
checkNotNull(subscribers);
Queue<Event> queueForThread = queue.get();
queueForThread.offer(new Event(event, subscribers));
if (!dispatching.get()) {
dispatching.set(true);
try {
Event nextEvent;
while ((nextEvent = queueForThread.poll()) != null) {
while (nextEvent.subscribers.hasNext()) {
nextEvent.subscribers.next().dispatchEvent(nextEvent.event);
}
}
} finally {
dispatching.remove();
queue.remove();
}
}
}
示例7: levelOrder
import java.util.Queue; //導入方法依賴的package包/類
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new LinkedList<>();
if (root == null) return list;
Queue<TreeNode> q = new LinkedList<>();
List<Integer> l = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode t = q.poll();
l.add(t.val);
if (t.left != null) q.offer(t.left);
if (t.right != null) q.offer(t.right);
}
list.add(l);
l = new LinkedList<>();
}
return list;
}
示例8: getTimes
import java.util.Queue; //導入方法依賴的package包/類
private static long[] getTimes(Track track, long[] syncSamples, long targetTimeScale) {
long[] syncSampleTimes = new long[syncSamples.length];
Queue<TimeToSampleBox.Entry> timeQueue = new LinkedList<TimeToSampleBox.Entry>(track.getDecodingTimeEntries());
int currentSample = 1; // first syncsample is 1
long currentDuration = 0;
long currentDelta = 0;
int currentSyncSampleIndex = 0;
long left = 0;
while (currentSample <= syncSamples[syncSamples.length - 1]) {
if (currentSample++ == syncSamples[currentSyncSampleIndex]) {
syncSampleTimes[currentSyncSampleIndex++] = (currentDuration * targetTimeScale) / track.getTrackMetaData().getTimescale();
}
if (left-- == 0) {
TimeToSampleBox.Entry entry = timeQueue.poll();
left = entry.getCount() - 1;
currentDelta = entry.getDelta();
}
currentDuration += currentDelta;
}
return syncSampleTimes;
}
示例9: inLevelOrder
import java.util.Queue; //導入方法依賴的package包/類
private static String inLevelOrder(TreeNode t) {
StringBuilder builder = new StringBuilder();
Queue<TreeNode> q = new LinkedList<>();
q.add(t);
while (!q.isEmpty()) {
t = q.poll();
if (null == t) continue;
builder.append(t.getValue());
q.add(t.getLeft());
q.add(t.getRight());
}
return builder.toString();
}
示例10: messageSent
import java.util.Queue; //導入方法依賴的package包/類
@Override
public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
T stream = getMessageClass().cast(session.getAttribute(CURRENT_STREAM));
if (stream == null) {
nextFilter.messageSent(session, writeRequest);
} else {
IoBuffer buffer = getNextBuffer(stream);
if (buffer == null) {
// End of stream reached.
session.removeAttribute(CURRENT_STREAM);
WriteRequest currentWriteRequest = (WriteRequest) session.removeAttribute(CURRENT_WRITE_REQUEST);
// Write queued WriteRequests.
Queue<WriteRequest> queue = removeWriteRequestQueue(session);
if (queue != null) {
WriteRequest wr = queue.poll();
while (wr != null) {
filterWrite(nextFilter, session, wr);
wr = queue.poll();
}
}
currentWriteRequest.getFuture().setWritten();
nextFilter.messageSent(session, currentWriteRequest);
} else {
nextFilter.filterWrite(session, new DefaultWriteRequest(buffer));
}
}
}
示例11: displayLevelOrder
import java.util.Queue; //導入方法依賴的package包/類
/**
* level by level display of the nodes (starts from left to right for nodes on the same level)
*
* @implSpec This method is not recursive.
* @implNote Hint: Use a local queue to store the children of the current node.
*/
public static void displayLevelOrder(TreeNode t) {
Queue<TreeNode> q = new LinkedList<>();
q.add(t);
while (null != (t = q.poll())) {
System.out.print(t.getValue());
if (null != t.getLeft()) q.add(t.getLeft());
if (null != t.getRight()) q.add(t.getRight());
}
}
示例12: main
import java.util.Queue; //導入方法依賴的package包/類
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Queue<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < 5; i++) {
queue.add(Integer.parseInt(scanner.nextLine()));
}
Drawable circle = new Circle(queue.poll(), queue.poll(), queue.poll());
Drawable rect = new Rectangle(queue.poll(), queue.poll());
circle.draw();
rect.draw();
}
示例13: createPolicyAlternatives
import java.util.Queue; //導入方法依賴的package包/類
/**
* Method creates policy alternatives according to provided model. The model structure is modified in the process.
*
* @return created policy alternatives resulting from policy source model.
*/
private Collection<AssertionSet> createPolicyAlternatives(final PolicySourceModel model) throws PolicyException {
// creating global method variables
final ContentDecomposition decomposition = new ContentDecomposition();
// creating processing queue and starting the processing iterations
final Queue<RawPolicy> policyQueue = new LinkedList<RawPolicy>();
final Queue<Collection<ModelNode>> contentQueue = new LinkedList<Collection<ModelNode>>();
final RawPolicy rootPolicy = new RawPolicy(model.getRootNode(), new LinkedList<RawAlternative>());
RawPolicy processedPolicy = rootPolicy;
do {
Collection<ModelNode> processedContent = processedPolicy.originalContent;
do {
decompose(processedContent, decomposition);
if (decomposition.exactlyOneContents.isEmpty()) {
final RawAlternative alternative = new RawAlternative(decomposition.assertions);
processedPolicy.alternatives.add(alternative);
if (!alternative.allNestedPolicies.isEmpty()) {
policyQueue.addAll(alternative.allNestedPolicies);
}
} else { // we have a non-empty collection of exactly ones
final Collection<Collection<ModelNode>> combinations = PolicyUtils.Collections.combine(decomposition.assertions, decomposition.exactlyOneContents, false);
if (combinations != null && !combinations.isEmpty()) {
// processed alternative was split into some new alternatives, which we need to process
contentQueue.addAll(combinations);
}
}
} while ((processedContent = contentQueue.poll()) != null);
} while ((processedPolicy = policyQueue.poll()) != null);
// normalize nested policies to contain single alternative only
final Collection<AssertionSet> assertionSets = new LinkedList<AssertionSet>();
for (RawAlternative rootAlternative : rootPolicy.alternatives) {
final Collection<AssertionSet> normalizedAlternatives = normalizeRawAlternative(rootAlternative);
assertionSets.addAll(normalizedAlternatives);
}
return assertionSets;
}
示例14: flush
import java.util.Queue; //導入方法依賴的package包/類
@Override
public void flush() {
if (this.mMessageBuffer.isEmpty()) {
return;
}
final Queue<String> loggedMessages = readLoggedMessages();
try (final FileWriter writer = new FileWriter(FILEPATH, false)) {
writer.write(getHeadMessage());
writer.write(this.mLineSeparator);
final Queue<String> messagesToWrite = mixBufferIntoMessages(loggedMessages, this.mMessageBuffer);
this.mMessageBuffer.clear();
while (!messagesToWrite.isEmpty()) {
final String message = messagesToWrite.poll();
writer.write(message);
if (!message.endsWith(this.mLineSeparator)) {
writer.write(this.mLineSeparator);
}
}
writer.write(getTailMessage());
writer.flush();
} catch (final IOException e) {
throw new LoggerCanNotWriteException(e);
}
}
示例15: runAll
import java.util.Queue; //導入方法依賴的package包/類
void runAll(Queue<CmdItem> queue) {
synchronized (queue) {
while (!queue.isEmpty()) {
CmdItem cmdItem = queue.poll();
switch (cmdItem.cmdId) {
case CMD_PROCESS_FRAME:
processFrame((byte[]) cmdItem.param1, (Camera) cmdItem.param2);
break;
case CMD_SETUP_SURFACE_TEXTURE:
setUpSurfaceTextureInternal((Camera) cmdItem.param1, (byte[]) cmdItem.param2);
break;
case CMD_SET_FILTER:
setFilterInternal((GPUImageFilterGroupBase) cmdItem.param1);
break;
case CMD_RERUN_ONDRAW_RUNNABLE:
((Runnable) cmdItem.param1).run();
break;
case CMD_RERUN_DRAWEND_RUNNABLE:
((Runnable) cmdItem.param1).run();
break;
default:
throw new RuntimeException("can't find command");
}
cmdItem.param1 = cmdItem.param2 = null;
mCmdItemCacher.cache(cmdItem);
}
}
}