LinkedBlockingQueue的toString()方法返回LinkedBlockingQueue元素的String表示形式。 LinkedBlockingQueue字符串包含从first(head)到last(tail)的元素,并以正确的顺序括在方括号(“[]”)中。元素由字符“,”(逗号和空格)分隔。因此,本质上使用toString()方法将LinkedBlockingQueue的所有元素转换为String表示形式。
此方法覆盖类AbstractCollection中的toString()
用法:
public String toString()
返回值:此方法返回一个String,它表示从第一个(头)到最后一个(尾)的LinkedBlockingQueue元素的表示形式,并以正确的顺序括在方括号(“[]”)中,并以``,”(逗号和空格)分隔。
以下示例程序旨在说明LinkedBlockingQueue类的toString()方法:
示例1:
// Java Program Demonstrate toString()
// 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<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add(2300);
linkedQueue.add(1322);
linkedQueue.add(8945);
linkedQueue.add(6512);
// toString() on linkedQueue
String queueRepresentation = linkedQueue.toString();
// print results
System.out.println("Queue Representation:");
System.out.println(queueRepresentation);
}
}
输出:
Queue Representation: [2300, 1322, 8945, 6512]
// Java Program Demonstrate toString()
// 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.stringRepresentation();
}
public void stringRepresentation()
{
// define capacity of LinkedBlockingQueue
int capacity = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(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);
// toString() on linkedQueue
String queueRepresentation = linkedQueue.toString();
// print results
System.out.println("Queue Representation:");
System.out.println(queueRepresentation);
}
}
输出:
Queue Representation: [Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toString–
相关用法
- Java LinkedBlockingQueue toString()用法及代码示例
- Java LinkedBlockingQueue take()用法及代码示例
- Java LinkedBlockingQueue put()用法及代码示例
- Java 8 LinkedBlockingQueue spliterator()用法及代码示例
- Java MonthDay toString()用法及代码示例
- Java Optional toString()用法及代码示例
- Java ZoneOffset toString()用法及代码示例
- Java Scanner toString()用法及代码示例
- Java Instant toString()用法及代码示例
- Java SimpleTimeZone toString()用法及代码示例
- Java LinkedTransferQueue toString()用法及代码示例
- Java BigDecimal toString()用法及代码示例
- Java Byte toString()用法及代码示例
- Java BitSet toString()用法及代码示例
- Java Calendar toString()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LinkedBlockingQueue toString() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。