当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayBlockingQueue peek()用法及代码示例


ArrayBlockingQueue是有界的阻塞队列,该队列内部使用数组支持的元素存储。

  • ArrayBlockingQueue类是Java Collections Framework的成员。
  • 有界意味着它将具有固定的大小,您不能存储数量超过队列容量的元素。
  • 队列还遵循FIFO(先进先出)规则,用于存储和删除队列中的元素。
  • 如果您尝试将一个元素放入一个完整的队列或从一个空队列中取出一个元素,那么该队列将阻塞。

peek()方法用于返回队列的头部。它检索但不删除此队列的头。如果队列为空,则此方法返回null。

用法:


public E peek()

参量:该方法不带任何参数。

返回值:该方法返回存在于此队列开头的元素。

下面的程序说明ArrayBlockingQueue的peek()方法。
示例1:

// 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 addding numbers queue is "); 
    System.out.println(queue); 
  
    // Print head of queue using peek() method 
    System.out.println("Head of queue "+queue.peek()); 
      
}  
}
输出:
After addding numbers queue is 
[23, 32, 45, 12]
Head of queue 23

示例2:

// Program to demonstrate contains(Object o) 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 contains 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); 
    } 
}
输出:
Details of First User Inserted
User Name : Aman
User Age : 24

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayBlockingQueue peek() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。