本文整理匯總了Java中java.util.Queue.size方法的典型用法代碼示例。如果您正苦於以下問題:Java Queue.size方法的具體用法?Java Queue.size怎麽用?Java Queue.size使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Queue
的用法示例。
在下文中一共展示了Queue.size方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: averageOfLevels
import java.util.Queue; //導入方法依賴的package包/類
public List<Double> averageOfLevels(TreeNode node) {
List<Double> list = new LinkedList<>();
if (node == null) return list;
Queue<TreeNode> q = new LinkedList<>();
q.offer(node);
while (!q.isEmpty()) {
int size = q.size();
double sum = 0;
int i = 0;
while (i < size) {
TreeNode t = q.poll();
sum += t.val;
if (t.left != null) q.offer(t.left);
if (t.right != null) q.offer(t.right);
i++;
}
list.add(sum / size);
}
return list;
}
示例2: rightSideView
import java.util.Queue; //導入方法依賴的package包/類
public List<Integer> rightSideView(TreeNode root) {
if (root == null) return new LinkedList<>();
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();
if (i == 0) l.add(t.val);
if (t.right != null) {
q.offer(t.right);
}
if (t.left != null) {
q.offer(t.left);
}
}
}
return l;
}
示例3: free
import java.util.Queue; //導入方法依賴的package包/類
private void free(ByteBuffer oldBuf) {
if ((oldBuf == null) || ((maxCachedBufferSize != 0) && (oldBuf.capacity() > maxCachedBufferSize))
|| oldBuf.isReadOnly() || isDerived() || (Thread.currentThread() != ownerThread)) {
return;
}
// Add to the cache.
Queue<CachedBuffer> pool;
if (oldBuf.isDirect()) {
pool = directBuffers.get().get(oldBuf.capacity());
} else {
pool = heapBuffers.get().get(oldBuf.capacity());
}
if (pool == null) {
return;
}
// Restrict the size of the pool to prevent OOM.
if ((maxPoolSize == 0) || (pool.size() < maxPoolSize)) {
pool.offer(new CachedBuffer(oldBuf));
}
}
示例4: createEmojiAndTextNode
import java.util.Queue; //導入方法依賴的package包/類
/**
* 將字符串解析分離成emoji圖片節點和文本節點
* @param input 輸入字符串
* @return
*/
public static Node[] createEmojiAndTextNode(String input) {
Queue<Object> queue = EmojiHandler.getInstance().toEmojiAndText(input);
Node[] nodes = new Node[queue.size()];
int i = 0;
while (!queue.isEmpty()) {
Object ob = queue.poll();
if (ob instanceof String) {
String text = (String) ob;
nodes[i++] = createTextNode(text);
} else if (ob instanceof Emoji) {
Emoji emoji = (Emoji) ob;
nodes[i++] = createEmojiNode(emoji, 24, 10);
}
}
return nodes;
}
示例5: add
import java.util.Queue; //導入方法依賴的package包/類
public void add(final Stock stock){
final Queue<OhlcContainer> sequence = this.sequences.get(stock.getCode());
final OhlcContainer latest = sequence.peek();
if ((latest == null) || (latest.getTime().isBefore(stock.getTime()) && latest.getClose() != stock.getClose())) {
getLogger().debug("New {} stock {} passed for average processing", stock.getCode(), stock);
if (sequence.size() == 10)
sequence.remove();
sequence.add(stock);
this.values.replace(stock.getCode(), sequence.stream()
.mapToDouble(OhlcContainer::getClose)
.sum() / 10);
}
}
示例6: size
import java.util.Queue; //導入方法依賴的package包/類
@Override
public int size() {
int total = 0;
for (final Queue queue: messageBuffer.values()) {
total += queue.size();
}
return total;
}
示例7: search
import java.util.Queue; //導入方法依賴的package包/類
public static Node search(Node root, int taskId) {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (queue.size() > 0) {
Node current = queue.poll();
if (taskId >= 0 && current.getTaskId() == taskId) {
return current;
} else {
queue.addAll(current.getChildren());
}
}
return null;
}
示例8: countBuffersOfSize
import java.util.Queue; //導入方法依賴的package包/類
/**
* Return the number of available buffers of a given size.
* This is used only for tests.
*/
@VisibleForTesting
int countBuffersOfSize(int size) {
Queue<WeakReference<ByteBuffer>> list = buffersBySize.get(size);
if (list == null) {
return 0;
}
return list.size();
}
示例9: count
import java.util.Queue; //導入方法依賴的package包/類
public int count() {
Queue<Object> q = this.queue;
if (q == null) {
return 0;
}
return q.size();
}
示例10: computeOperations
import java.util.Queue; //導入方法依賴的package包/類
/**
* Compute Operations with values
*/
protected float computeOperations(Queue<Float> values, Queue<String> operations) {
String operation;
float calculation = 0;
// base case when no operators on screen
if (operations.size() == 0 && values.size() == 1) {
return values.remove();
}
int i = 0;
while (operations.size() > 0) {
operation = operations.remove();
switch (operation) {
case MINUS_CHAR:
//first iteration two values can be used at the same time
if (i == 0 && values.size() >= 2) {
calculation = computeSubsctraction(values.remove(), values.remove());
} else if (values.size() > 0) {
calculation = computeSubsctraction(calculation, values.remove());
}
break;
case PLUS_CHAR:
default:
//first iteration two values can be used at the same time
if (i == 0 && values.size() >= 2) {
calculation = computeAddition(values.remove(), values.remove());
} else if (values.size() > 0) {
calculation = computeAddition(calculation, values.remove());
}
break;
}
i++;
}
return calculation;
}
示例11: getGameElementCount
import java.util.Queue; //導入方法依賴的package包/類
@Override
public synchronized int getGameElementCount() {
int size = 0;
for (Queue<GameElement> queue : this.gameElements) {
size += queue.size();
}
return size;
}
示例12: bfsTraverse
import java.util.Queue; //導入方法依賴的package包/類
/**
* <strong>使用廣度優先搜索收集</strong>當前實體及其所有子孫實體到集合,葉子實體排在最前麵<br/>
* 這樣才能保證先刪除子孫節點,後刪除父節點,避免違反外鍵約束
* @param currentEntity 存放當前實體及其所有子孫實體的集合
* @param treeEntities 所有實體的集合
* @return 當前實體及其所有子孫實體的集合,葉子實體排在集合最前麵
*/
@SuppressWarnings("unchecked")
@Transactional
private LinkedList<SelfReference<T>> bfsTraverse(SelfReference<T> currentEntity, LinkedList<SelfReference<T>> treeEntities) {
// 無法獲取子孫類目時,在事務中重新獲取當前節點
try {
currentEntity.getChildren().size();
} catch (LazyInitializationException e) {
ID id = (ID) ((BaseEntity)currentEntity).getId();
currentEntity = (SelfReference<T>) findOne(id);
}
// 隊列,用於廣度優先遍曆。每一時刻,隊列所包含的節點是那些本身已經被訪問,而它的鄰居(這裏即子節點)還有未被訪問的節點
Queue<SelfReference<T>> queue = new ArrayDeque<>();
treeEntities.addFirst(currentEntity); // 對當前遍曆到的元素執行的操作:加在頭部,後續用於刪除節點(避免違反外鍵約束)
queue.add(currentEntity); // 加在尾部
while (queue.size() != 0) { // 直到隊列為空
SelfReference<T> parent = queue.remove(); // 移除在隊列頭部的節點
if(parent.getChildren().size() != 0) {
parent.getChildren().forEach(child -> {
treeEntities.addFirst((SelfReference<T>) child); // 對當前遍曆到的元素執行的操作:加在頭部
queue.add((SelfReference<T>) child); // 加在尾部
});
}
}
return treeEntities;
}
示例13: remove
import java.util.Queue; //導入方法依賴的package包/類
private static void remove(Iterator<Integer> it,
Queue<Integer> q) {
int size = q.size();
it.remove();
removeIsCurrentlyIllegal(it);
equal(size, q.size()+1);
}
示例14: onReceiveComplete
import java.util.Queue; //導入方法依賴的package包/類
@Override
public void onReceiveComplete(int id, int e, MPIBuffer buffer) {
lock.lock();
try {
// we need to try to build the message here, we may need many more messages to complete
// LOG.info(String.format("%d received message from %d", executor, id));
MPIMessage currentMessage = currentMessages.get(id);
ByteBuffer byteBuffer = buffer.getByteBuffer();
byteBuffer.position(buffer.getSize());
byteBuffer.flip();
if (currentMessage == null) {
currentMessage = new MPIMessage(id, type, MPIMessageDirection.IN, this);
if (isKeyed) {
currentMessage.setKeyType(keyType);
}
currentMessages.put(id, currentMessage);
MessageHeader header = messageDeSerializer.buildHeader(buffer, e);
// LOG.info(String.format("%d header source %d length %d", executor,
// header.getSourceId(), header.getLength()));
currentMessage.setHeader(header);
currentMessage.setHeaderSize(16);
}
// lets rewind to 0
currentMessage.addBuffer(buffer);
currentMessage.build();
if (currentMessage.isComplete()) {
// LOG.info(String.format("%d completed recv ", executor));
currentMessages.remove(id);
Queue<MPIMessage> deserializeQueue = pendingReceiveDeSerializations.get(id);
if (!deserializeQueue.offer(currentMessage)) {
throw new RuntimeException(executor + " We should have enough space: "
+ deserializeQueue.size());
}
}
} finally {
lock.unlock();
}
}
示例15: takeQueue
import java.util.Queue; //導入方法依賴的package包/類
private static <V> List<V> takeQueue(Queue<V> queue) {
int size = queue.size();
if (size == 0) {
return null;
}
List<V> result = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
result.add(queue.poll());
}
return result;
}