本文整理汇总了Java中java.util.Deque.peekLast方法的典型用法代码示例。如果您正苦于以下问题:Java Deque.peekLast方法的具体用法?Java Deque.peekLast怎么用?Java Deque.peekLast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Deque
的用法示例。
在下文中一共展示了Deque.peekLast方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryAppend
import java.util.Deque; //导入方法依赖的package包/类
/**
* Try to append to a ProducerBatch.
*
* If it is full, we return null and a new batch is created. We also close the batch for record appends to free up
* resources like compression buffers. The batch will be fully closed (ie. the record batch headers will be written
* and memory records built) in one of the following cases (whichever comes first): right before send,
* if it is expired, or when the producer is closed.
*/
// 查找batches集合对应队列的最后一个ProducerBatch
private RecordAppendResult tryAppend(long timestamp, byte[] key, byte[] value, Header[] headers, Callback callback, Deque<ProducerBatch> deque) {
//拿到消息队列中的最后一个
ProducerBatch last = deque.peekLast();
if (last != null) {
//调用ProducerBatch的tryAppend方法返回 FutureRecordMetadata future,MemoryRecordsBuilder中是还有空间
FutureRecordMetadata future = last.tryAppend(timestamp, key, value, headers, callback, time.milliseconds());
if (future == null)
last.closeForRecordAppends();
else
return new RecordAppendResult(future, deque.size() > 1 || last.isFull(), false);
}
return null;
}
示例2: getNodesWithTimedOutRequests
import java.util.Deque; //导入方法依赖的package包/类
/**
* Returns a list of nodes with pending in-flight request, that need to be timed out
*
* @param now current time in milliseconds
* @param requestTimeout max time to wait for the request to be completed
* @return list of nodes
*/
public List<String> getNodesWithTimedOutRequests(long now, int requestTimeout) {
List<String> nodeIds = new LinkedList<>();
for (Map.Entry<String, Deque<NetworkClient.InFlightRequest>> requestEntry : requests.entrySet()) {
String nodeId = requestEntry.getKey();
Deque<NetworkClient.InFlightRequest> deque = requestEntry.getValue();
if (!deque.isEmpty()) {
NetworkClient.InFlightRequest request = deque.peekLast();
long timeSinceSend = now - request.sendTimeMs;
if (timeSinceSend > requestTimeout)
nodeIds.add(nodeId);
}
}
return nodeIds;
}
示例3: getTrailingAverage
import java.util.Deque; //导入方法依赖的package包/类
private Integer getTrailingAverage(Deque<Entry<Long, Long>> list, int seconds) {
Entry<Long,Long> latest = list.peekLast();
Entry<Long,Long> old = list.peekFirst();
Iterator<Entry<Long,Long>> iter = list.descendingIterator();
while (iter.hasNext()) {
Entry<Long,Long> e = iter.next();
if (e.getKey() - latest.getKey() > seconds * ONE_BILLION) {
old = e;
break;
}
}
final long oldKey = old.getKey();
final long latestKey = latest.getKey();
if (oldKey == latestKey) {
return null;
} else {
return (int) (100 * (old.getValue() - latest.getValue()) / (oldKey - latestKey));
}
}
示例4: maxSlidingWindow
import java.util.Deque; //导入方法依赖的package包/类
public int[] maxSlidingWindow(int[] nums, int k) {
int len = nums.length;
if (len == 0) return nums;
int[] res = new int[len - k + 1];
Deque<Integer> dq = new LinkedList<Integer>();
for (int i = 0; i < len; i++) {
while (!dq.isEmpty() && dq.peekLast() < nums[i])
dq.pollLast();
dq.offer(nums[i]);
if (i >= k - 1) {
res[i - k + 1] = dq.peek();
if (nums[i - k + 1] == dq.peek())
dq.pollFirst();
}
}
return res;
}
示例5: maxSlidingWindow
import java.util.Deque; //导入方法依赖的package包/类
public static int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> deque = new ArrayDeque<>();
int n = nums.length;
int[] res = new int[n - k + 1];
for (int i = 0; i < n; i++) {
if (!deque.isEmpty() && deque.peek() == i - k) deque.poll();
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) deque.pollLast();
deque.offer(i);
if (i >= k - 1) {
res[i + 1 - k] = nums[deque.peek()];
}
}
return res;
}
示例6: deleteDirectoryContents
import java.util.Deque; //导入方法依赖的package包/类
/**
* Wipes out content of a directory starting from a given directory.
*
* @param directory to be cleaned
* @param retain if true, the given directory will be retained.
* @throws IOException
*/
public static void deleteDirectoryContents(File directory, boolean retain) throws IOException {
if (!directory.isDirectory()) {
throw new IOException("Not a directory: " + directory);
}
// avoid using guava's Queues.newArrayDeque() since this is a utility class that can be used in
// all sorts of
// contexts, some of which may use clashing guava versions... For example, when explore launches
// a Hive query,
// it includes hive-exec.jar which bundles guava 11 in its jar...
Deque<File> stack = new ArrayDeque<>();
stack.addAll(listFiles(directory));
while (!stack.isEmpty()) {
File file = stack.peekLast();
List<File> files = listFiles(file);
if (files.isEmpty()) {
if (!file.delete()) {
throw new IOException("Failed to delete file " + file);
}
stack.pollLast();
} else {
stack.addAll(files);
}
}
if (!retain) {
if (!directory.delete()) {
throw new IOException("Failed to delete directory " + directory);
}
}
}
示例7: maxSlidingWindow
import java.util.Deque; //导入方法依赖的package包/类
public int[] maxSlidingWindow(int[] nums, int k) {
if (k == 0) {
return new int[0];
}
int n = nums.length;
int[] result = new int[n - k + 1];
int current = 0;
Deque<Integer> deque = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
while (!deque.isEmpty() && deque.peek() < i - k + 1) {
deque.poll();
}
while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
deque.pollLast();
}
deque.offer(i);
if (i >= k - 1) { // allowing sliding window to fit in full initially
result[current++] = nums[deque.peek()];
}
}
return result;
}
示例8: getLast
import java.util.Deque; //导入方法依赖的package包/类
public FormValue getLast(String name) {
final Deque<FormValue> deque = values.get(name);
return deque == null ? null : deque.peekLast();
}
示例9: convertPendingTransactionsToMessages
import java.util.Deque; //导入方法依赖的package包/类
Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
if (cohortEntry == null) {
continue;
}
final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
@Override
protected BatchedModifications getModifications() {
final BatchedModifications lastBatch = newMessages.peekLast();
if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
return lastBatch;
}
// Allocate a new message
final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
cohortEntry.getClientVersion());
newMessages.add(ret);
return ret;
}
});
final BatchedModifications last = newMessages.peekLast();
if (last != null) {
final boolean immediate = cohortEntry.isDoImmediateCommit();
last.setDoCommitOnReady(immediate);
last.setReady(true);
last.setTotalMessagesSent(newMessages.size());
messages.addAll(newMessages);
if (!immediate) {
switch (cohort.getState()) {
case CAN_COMMIT_COMPLETE:
case CAN_COMMIT_PENDING:
messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
cohortEntry.getClientVersion()));
break;
case PRE_COMMIT_COMPLETE:
case PRE_COMMIT_PENDING:
messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
cohortEntry.getClientVersion()));
break;
default:
break;
}
}
}
}
return messages;
}