當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LinkedBlockingQueue toString()用法及代碼示例


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–



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LinkedBlockingQueue toString() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。