Java中的LinkedBlockingQueue toString()方法

这个 toString() 方法 LinkedBlockingQueue 返回此集合的字符串表示形式。字符串表示法由集合元素的列表组成,这些元素按迭代器返回的顺序排列,用方括号括起来 (“[]”) .相邻元素由字符分隔 “, “ (逗号和空格)。元素被转换为字符串,就像字符串一样。(物体)的价值。

null

语法:

public String toString()

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

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

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

项目1:

// 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<Integer> LBQ
= new LinkedBlockingQueue<Integer>();
// 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]

项目2:

// 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<String> LBQ
= new LinkedBlockingQueue<String>();
// 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toString–

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享