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


Java LinkedBlockingDeque size()用法及代码示例


LinkedBlockingDeque的size()方法返回Deque容器的当前大小。在调用该函数时,将返回Deque容器中的元素数量。如果容器受容量限制,则它还会返回函数调用时容器中存在的元素数。

用法:

public int size()

返回值:此方法返回一个整数值,该值表示容器中的元素数。


以下示例程序旨在说明LinkedBlockingDeque的size()方法:

示例1:

// Java Program to demonstrate public int size() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add(15); 
        LBD.add(20); 
        LBD.add(20); 
        LBD.add(15); 
        LBD.add(15); 
        LBD.add(20); 
        LBD.add(20); 
        LBD.add(15); 
  
        // print Dequeue 
        System.out.println("Linked Blocking Deque: " + LBD); 
  
        // prints the Deque after removal 
        System.out.println("Size of Linked Blocking Deque: "
                           + LBD.size()); 
    } 
}
输出:
Linked Blocking Deque: [15, 20, 20, 15, 15, 20, 20, 15]
Size of Linked Blocking Deque: 8

示例2:

// Java Program to demonstrate public int size() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<String> LBD 
            = new LinkedBlockingDeque<String>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add("geeks"); 
        LBD.add("forGeeks"); 
        LBD.add("A Computer"); 
        LBD.add("Portal"); 
  
        // print Dequeue 
        System.out.println("Linked Blocking Deque: " + LBD); 
  
        // prints the Deque after removal 
        System.out.println("Size of Linked Blocking Deque: "
                           + LBD.size()); 
    } 
}
输出:
Linked Blocking Deque: [geeks, forGeeks, A Computer, Portal]
Size of Linked Blocking Deque: 4

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#size–



相关用法


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