java+linked借口,Java LinkedBlockingDeque offer()用法及代码示例

LinkedBlockingDeque的offer(E e)方法将参数中传递的元素插入到Deque的末尾。如果超出了容器的容量,则不会像add()和addFirst()函数一样返回异常。

用法:

public boolean offer(E e)

参数:此方法接受强制参数e,该参数是要在LinkedBlockingDeque末尾插入的元素。

返回值:如果已插入元素,则此方法返回true,否则返回false。

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

示例1:

// Java Program Demonstrate offer()

// method of LinkedBlockingDeque

import java.util.concurrent.LinkedBlockingDeque;

import java.util.*;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of LinkedBlockingDeque

LinkedBlockingDeque LBD

= new LinkedBlockingDeque(4);

// Add numbers to end of LinkedBlockingDeque

LBD.offer(7855642);

LBD.offer(35658786);

LBD.offer(5278367);

LBD.offer(74381793);

// Cannot be inserted

LBD.offer(10);

// cannot be inserted hence returns false

if (!LBD.offer(10))

System.out.println("The element 10 cannot be inserted"+

" as capacity is full");

// before removing print queue

System.out.println("Linked Blocking Deque: " + LBD);

}

}

输出:

The element 10 cannot be inserted as capacity is full

Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]

示例2:

// Java Program Demonstrate offer()

// method of LinkedBlockingDeque

import java.util.concurrent.LinkedBlockingDeque;

import java.util.*;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of LinkedBlockingDeque

LinkedBlockingDeque LBD

= new LinkedBlockingDeque(4);

// Add numbers to end of LinkedBlockingDeque

LBD.offer("abc");

LBD.offer("gopu");

LBD.offer("geeks");

LBD.offer("richik");

// Cannot be inserted

LBD.offer("hii");

// cannot be inserted hence returns false

if (!LBD.offer("hii"))

System.out.println("The element 'hii' cannot be inserted"+

" as capacity is full");

// before removing print queue

System.out.println("Linked Blocking Deque: " + LBD);

}

}

输出:

The element 'hii' cannot be inserted as capacity is full

Linked Blocking Deque: [abc, gopu, geeks, richik]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值