java queue contains_Java LinkedBlockingQueue contains()用法及代码示例

如果队列包含作为参数传递的对象obj,则LinkedBlockingQueue的contains(Object obj)方法将返回true。当且仅当此队列包含至少一个元素e等于等于作为参数传递的对象o即e.equals(o)时,它返回true。此方法对于检查队列中是否包含元素非常有帮助。

用法:

public boolean contains(Object o)

参数:此方法采用强制参数o,该参数是要在队列中检查的对象。

返回值:如果此队列包含作为参数传递的对象,则此方法返回true。否则返回false。

下面的程序说明LinkedBlockingQueue类的contains()方法:

示例1:检查数字队列中是否包含数字n。数字n作为输入传递给LinkedBlockingQueue的contains()。

// Java Program Demonstrate contains(Object o)

// method of LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public static void main(String[] args)

{

// define capacity of LinkedBlockingQueue

int capacityOfQueue = 50;

// create object of LinkedBlockingQueue

LinkedBlockingQueue linkedQueue

= new LinkedBlockingQueue(capacityOfQueue);

// Add element to LinkedBlockingQueue

linkedQueue.add(2300);

linkedQueue.add(1322);

linkedQueue.add(8945);

linkedQueue.add(6512);

// print LinkedQueue

System.out.println("linkedQueue: " + linkedQueue);

// check whether LinkedBlockingQueue contains 6512

boolean response1 = linkedQueue.contains(6512);

// print result

System.out.println("LinkedBlockingQueue contains "

+ "number 6512 : "

+ response1);

// check whether LinkedBlockingQueue contains 5324

boolean response2 = linkedQueue.contains(5324);

// print result

System.out.println("LinkedBlockingQueue contains"

+ " number 5324 : "

+ response2);

}

}

输出:

linkedQueue: [2300, 1322, 8945, 6512]

LinkedBlockingQueue contains number 6512 : true

LinkedBlockingQueue contains number 5324 : false

示例2:检查员工队列中是否包含某些员工e。将雇员e的详细信息作为输入传递给LinkedBlockingQueue的contains()。

// Java Program Demonstrate contains(Object o)

// method of LinkedBlockingQueue.

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

// create an Employee Object with

// position and salary as an attribute

public class Employee {

public String name;

public String position;

public String salary;

Employee(String name, String position, String salary)

{

this.name = name;

this.position = position;

this.salary = salary;

}

@Override

public String toString()

{

return "Employee [name=" + name + ", position="

+ position + ", salary=" + salary + "]";

}

}

// Main Method

public static void main(String[] args)

{

GFG gfg = new GFG();

gfg.containsMethodExample();

}

public void containsMethodExample()

{

// define capacity of LinkedBlockingQueue

int capacity = 50;

// create object of LinkedBlockingQueue

LinkedBlockingQueue linkedQueue

= new LinkedBlockingQueue(capacity);

Employee emp1 = new Employee("Aman",

"Analyst",

"24000");

Employee emp2 = new Employee("Sachin",

"Developer",

"39000");

// Add Employee Objects to linkedQueue

linkedQueue.add(emp1);

linkedQueue.add(emp2);

// print LinkedQueue

System.out.println("linkedQueue: " + linkedQueue);

// Employee to be checked

Employee checkEmp1 = new Employee("Sanjeev",

"Tester",

"26000");

// check whether linkedQueue contains emp1

boolean response1 = linkedQueue.contains(emp1);

// print results

System.out.println("linkedQueue contains "

+ emp1.toString() + " : "

+ response1);

// check whether linkedQueue contains checkEmp1

boolean response2 = linkedQueue.contains(checkEmp1);

// print results

System.out.println("linkedQueue contains "

+ checkEmp1.toString() + " : "

+ response2);

}

}

输出:

linkedQueue:

[Employee [name=Aman, position=Analyst, salary=24000],

Employee [name=Sachin, position=Developer, salary=39000]]

linkedQueue contains Employee [name=Aman, position=Analyst, salary=24000] : true

linkedQueue contains Employee [name=Sanjeev, position=Tester, salary=26000] : false

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值