GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • ArrayBlockingQueue
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — ArrayBlockingQueue peek() Method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

ArrayBlockingQueue peek() Method in Java

Last Updated : 11 Jul, 2025

ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. 
 


  • ArrayBlockingQueue class is a member of the Java Collections Framework.

  • Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.

  • The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.

  • If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.


The peek() method is used to return the head of the queue. It retrieves but does not remove, the head of this queue. If the queue is empty then this method returns null.
Syntax: 
 

public E peek()


Parameters: The method does not take any parameters.
Return Value: The method returns the element present at the head of this queue.
Below programs illustrate peek() method of ArrayBlockingQueue. 
Program 1: 
 

Java
// Program to demonstrate peek() method of ArrayBlockingQueue

import java.util.concurrent.ArrayBlockingQueue;

public class GFG {

public static void main(String[] args) {
    // Define capacity of ArrayBlockingQueue
    int capacity = 5;
    
    // Create object of ArrayBlockingQueue
    ArrayBlockingQueue<Integer> queue = 
         new ArrayBlockingQueue<Integer>(capacity);
    
    // Add element to ArrayBlockingQueue
    queue.add(23);
    queue.add(32);
    queue.add(45);
    queue.add(12);
    
    // Print queue after adding numbers
    System.out.println("After adding numbers queue is ");
    System.out.println(queue);

    // Print head of queue using peek() method
    System.out.println("Head of queue "+queue.peek());
    
} 
}

Output: 
After adding numbers queue is 
[23, 32, 45, 12]
Head of queue 23

 

Program 2:

Java
// Program to demonstrate peek( ) method of ArrayBlockingQueue

import java.util.concurrent.ArrayBlockingQueue;

public class GFG {
    
    // Create a User Object with name and age as the attribute
    public class User{
        
        public String name;
        public String age;
        User(String name,String age){
            this.name=name;
            this.age=age;
        }
        
    }
    
    // Main Method
    public static void main(String[] args) {
        GFG gfg=new GFG();
        gfg.offerExample();
        
    } 
    
    // Method to give example of offer function
    public void offerExample() {
        
        // Define capacity of ArrayBlockingQueue
            int capacity = 5;
            
            // Create object of ArrayBlockingQueue
            ArrayBlockingQueue<User> queue =
                new ArrayBlockingQueue<User>(capacity);
            
            // Create user objects
            User user1=new User("Aman","24");
            User user2=new User("Amar","23");
            User user3=new User("Sanjeet","25");
            User user4=new User("Suvo","26");
            User user5=new User("Ravi","22"); 
            
            // Add Objects to ArrayBlockingQueue
            queue.offer(user1);
            queue.offer(user2);
            queue.offer(user3);
            queue.offer(user4);
            queue.offer(user5);
            
            // Find peek of queue
           User head=queue.peek();
          
            // Print head
            System.out.println("Details of First User Inserted");
            System.out.println("User Name : "+head.name);
            System.out.println("User Age : "+head.age);
    }
}

Output: 
Details of First User Inserted
User Name : Aman
User Age : 24

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#peek()

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.