📌  相关文章
📜  Java中的 LinkedBlockingQueue toString() 方法

📅  最后修改于: 2022-05-13 01:55:27.066000             🧑  作者: Mango

Java中的 LinkedBlockingQueue toString() 方法

LinkedBlockingQueuetoString()方法返回此集合的字符串表示形式。字符串表示由集合元素的列表组成,按其迭代器返回的顺序排列,括在方括号(“[]”)中。相邻元素由字符“、” (逗号和空格)分隔。通过 String.valueOf(Object) 将元素转换为字符串。

句法:

public String toString()

参数:此方法不接受任何参数。

返回:此方法返回此集合的字符串表示形式。

下面的程序说明了 LinkedBlockingQueue 的toString()方法:

方案一:

// Java Program Demonstrate toString()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue LBQ
            = new LinkedBlockingQueue();
  
        // Add numbers to end of LinkedBlockingQueue
        LBQ.add(7855642);
        LBQ.add(35658786);
        LBQ.add(5278367);
        LBQ.add(74381793);
  
        // Print the queue
        System.out.println("Linked Blocking Queue: " + LBQ);
  
        System.out.println("Linked Blocking Queue in string " 
                                           + LBQ.toString());
    }
}
输出:
Linked Blocking Queue: [7855642, 35658786, 5278367, 74381793]
Linked Blocking Queue in string [7855642, 35658786, 5278367, 74381793]

方案二:

// Java Program Demonstrate toString()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue LBQ
            = new LinkedBlockingQueue();
  
        // Add numbers to end of LinkedBlockingQueue
        LBQ.add("gopal");
        LBQ.add("gate");
        LBQ.add("GRE");
        LBQ.add("CAT");
  
        // Print the queue
        System.out.println("Linked Blocking Queue: " + LBQ);
  
        System.out.println("Linked Blocking Queue in string " 
                                            + LBQ.toString());
    }
}
输出:
Linked Blocking Queue: [gopal, gate, GRE, CAT]
Linked Blocking Queue in string [gopal, gate, GRE, CAT]

参考: https: Java/util/concurrent/LinkedBlockingQueue.html#toString–