Reputation: 23911
I want to create a queue of Strings that will have many identical elements in it (I'm simulating a set of program instructions). Is there any way to create this collection all at once with a single statement?
myQueue.addAll(create collection of x strings);
Otherwise, I'll obviously have to loop x times and call myqueue.add(String)
x times.
Just wondering if there is a one-line instead of 3-line way to do this...
Thanks!
Upvotes: 1
Views: 58
Reputation: 20929
Collections.nCopies(int n, T o)
returns a List<T>
containing n
copies of the given object o
.
Upvotes: 3
Reputation: 1434
myQueue.addAll(Arrays.asList(new String[]{"str", "str", "str"}));
Upvotes: 0