Java LinkedBlockingQueue toString()方法及示例

Java LinkedBlockingQueue toString()方法及示例

LinkedBlockingQueue的 toString() 方法返回LinkedBlockingQueue的元素的字符串表示。LinkedBlockingQueue的字符串包含了从第一个(head)到最后一个(tail)的元素,按照正确的顺序用方括号(”[]”)括起来。这些元素被字符’,’(逗号和空格)分开。所以基本上toString()方法是用来将LinkedBlockingQueue的所有元素转换为字符串表示。

这个方法覆盖了 **AbstractCollection **类中的toString()。

语法

public String toString()

返回值: 该方法返回一个字符串,表示LinkedBlockingQueue的元素,从第一个(head)到最后一个(tail),用方括号(“[]”)括起来,以’, ‘(逗号和空格)分开。

下面的程序说明了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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程