why do we need peek() when we have get() in linked list?.while I am studying the linked list I have this doubt. what is difference between them?
get()
requires an index to access an element, peek()
doesn't. As you can read in the comments the peek()
also comes from the Queue-Interface.
Simple said the peek()
internally ( see implementation )
get(0)
if the list is not empty null
if the list is empty The get(0)
itself doesn't return null
, it throws an java.lang.IndexOutOfBoundsException
instead.
LinkedList - Get vs Linked List - Peek
Take a look at the return values and thrown errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.