Java Deque 接口的 peek() 方法用于检索但不移除双端队列的头部。如果给定的双端队列为空,则该方法可能返回 null。
指定者:
- 查看接口 Queue<E>
用法:
E peek()
参数:
NA
返回:
上述方法用于返回由队列表示的双端队列的头部。另一方面,如果双端队列为空,则该方法可能返回 null。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekExample1 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.offer('J');
deque.offer('A');
deque.offer('V');
deque.offer('A');
deque.offer('T');
deque.offer('P');
deque.offer('O');
deque.offer('I');
deque.offer('N');
deque.offer('T');
Character val = deque.peek();
System.out.println("The first character is given as:"+val);
System.out.println("The deque is:");
for (Character character:deque) {
System.out.println(character);
}
}
}
输出:
The first character is given as:J The deque is: J A V A T P O I N T
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekExample2 {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.offer("Hello");
deque.offer(" ");
deque.offer("World");
String val = deque.peek();
System.out.println("The first string is given as:"+val);
System.out.println("The deque is:"+deque);
}
}
输出:
The first string is given as:Hello The deque is:[Hello, , World]
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekExample3 {
public static void main(String[] args) {
Deque<Long> deque = new ArrayDeque<>();
deque.offer(88l);
deque.offer(78l);
deque.offer(67l);
Long val = deque.peek();
System.out.println("The first long value is given as:"+val);
System.out.println("The resultant deque is:"+deque);
}
}
输出:
The first long value is given as:88 The resultant deque is:[88, 78, 67]
相关用法
- Java Deque peekLast()用法及代码示例
- Java Deque peekFirst()用法及代码示例
- Java Deque pollLast()用法及代码示例
- Java Deque poll()用法及代码示例
- Java Deque pop()用法及代码示例
- Java Deque pollFirst()用法及代码示例
- Java Deque push()用法及代码示例
- Java Deque size()用法及代码示例
- Java Deque remove()用法及代码示例
- Java Deque removeLast()用法及代码示例
- Java Deque addLast()用法及代码示例
- Java Deque addAll()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque add()用法及代码示例
- Java Deque removeFirstOccurrence()用法及代码示例
- Java Deque offerFirst()用法及代码示例
- Java Deque addFirst()用法及代码示例
- Java Deque offer()用法及代码示例
注:本文由纯净天空筛选整理自 Java Deque peek () Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。