Refers to a non-destructive operation performed on sequential, collection-like data structures that have the notion of "top element" or "next element", such as stacks, queues and streams. The `peek` operation returns the value of the "top" (or "next") element without "consuming" that element, i.e. without removing that element from the data structure.
Questions tagged [peek]
163 questions
214
votes
10 answers
In Java streams is peek really only for debugging?
I'm reading up about Java streams and discovering new things as I go along. One of the new things I found was the peek() function. Almost everything I've read on peek says it should be used to debug your Streams.
What if I had a Stream where each…

Adam.J
- 2,519
- 3
- 14
- 12
103
votes
18 answers
How to look ahead one element (peek) in a Python generator?
I can't figure out how to look ahead one element in a Python generator. As soon as I look it's gone.
Here is what I mean:
gen = iter([1,2,3])
next_value = gen.next() # okay, I looked forward and see that next_value = 1
# but now:
list(gen) # is…

bodacydo
- 75,521
- 93
- 229
- 319
75
votes
2 answers
Stream.peek() method in Java 8 vs Java 9
I am in the progress of learning through Java 8 lambda expressions and would like to ask about the following piece of Java code relating to the peek method in the function interface that I have come across.
On execution of the program on IDE, it…

Patrick C.
- 1,339
- 3
- 16
- 31
62
votes
2 answers
Peeking in a heap in python
What is the official way of peeking in a python heap as created by the heapq libs? Right now I have
def heappeak(heap):
smallest = heappop(heap)
heappush(heap, smallest)
return smallest
which is arguably, not very nice. Can I always assume…

Thomas
- 2,769
- 5
- 21
- 21
58
votes
7 answers
Can I get an item from a PriorityQueue without removing it yet?
I want to get the next item in queue but I don't want to dequeue it. Is it possible in Python's queue.PriorityQueue? From the docs, I don't see how can it be done

Jiew Meng
- 84,767
- 185
- 495
- 805
43
votes
7 answers
Reading specific number of bytes from a buffered reader in golang
I am aware of the specific function in golang from the bufio package.
func (b *Reader) Peek(n int) ([]byte, error)
Peek returns the next n bytes without advancing the reader. The bytes
stop being valid at the next read call. If Peek returns…

Kirk Backus
- 4,776
- 4
- 32
- 52
39
votes
8 answers
java BlockingQueue does not have a blocking peek?
I have a blocking queue of objects.
I want to write a thread that blocks till there is a object on the queue. Similar to the functionality provided by BlockingQueue.take().
However, since I do not know if I will be able to process the object…

rouble
- 16,364
- 16
- 107
- 102
34
votes
3 answers
C equivalent to fstream's peek
I know in C++, you're able to peek at the next character by using: in.peek();.
How would I go about this when trying to "peek" at the next character of a file in C?

Anthony
- 343
- 1
- 3
- 4
25
votes
3 answers
Is there any way to peek at the stdin buffer?
We know that stdin is, by default, a buffered input; the proof of that is in usage of any of the mechanisms that "leave data" on stdin, such as scanf():
int main()
{
char c[10] = {'\0'};
scanf("%9s", c);
printf("%s, and left is: %d\n",…

Mike
- 47,263
- 29
- 113
- 177
22
votes
8 answers
Can I peek on a BufferedReader?
Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek(). Thanks.

There is nothing we can do
- 23,727
- 30
- 106
- 194
18
votes
5 answers
C#: Implementing NetworkStream.Peek?
Currently, there isn't a NetworkStream.Peek method in C#. What is the best way of implementing such a method which functions just like NetworkStream.ReadByte except that the returned byte is not actually removed from the Stream?

Lopper
- 3,499
- 7
- 38
- 57
16
votes
6 answers
Is there a "HasNext" method for an IEnumerator?
With Java Iterators, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a "Peek" method.
My question: is there anything like a "hasNext" or "Peek" method with…

JaysonFix
- 2,515
- 9
- 28
- 28
14
votes
4 answers
Selecting specific object in queue ( ie peek +1)
if Peek returns the next object in a queue, is there a method I can use to get a specific object? For example, I want to find the third object in the queue and change one of its values?
right now Im just doing a foreach through the queue which…

Leroy Jenkins
- 2,680
- 6
- 25
- 33
14
votes
7 answers
Is StreamReader.Readline() really the fastest method to count lines in a file?
While looking around for a while I found quite a few discussions on how to figure out the number of lines in a file.
For example these three:
c# how do I count lines in a textfile
Determine the number of lines within a text file
How to count lines…

sergeidave
- 662
- 4
- 11
- 23
14
votes
4 answers
Get the number of bytes available in socket by 'recv' with 'MSG_PEEK' in C++
C++ has the following function to receive bytes from socket, it can check for number of bytes available with the MSG_PEEK flag. With MSG_PEEK, the returned value of 'recv' is the number of bytes available in socket:
#include
ssize_t…

jondinham
- 8,271
- 17
- 80
- 137