java:如何将All(Collection<>)添加到队列的前面?
public void traverse(Node root){
ArrayDeque<Node> queue = new ArrayDeque<Node>();
queue.add(root);
while(!queue.isEmpty()){
Node currentNode = queue.pollFirst();
List<Node> nl = getChildrenfromDB(currentNode);
queue.addAll(nl);
}
如何让 addAll(nl)
将整个集合(List
)添加到队列的前面?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,我一直在寻找同样的东西,这对我有用!
Actually I was looking for the same thing, and this worked for me!!
没有任何内置的东西。但它很容易模拟 - 只需以相反的顺序迭代列表并添加元素即可。这样他们就会以正确的顺序进入队列。
其他向后迭代的方法有:
LinkedList
的降序迭代器Collections.reverse(..)
选择适合您情况的一种。
There is nothing built-in. But it is simple to emulate - just iterate the list in reverse order and add the elements. That way they will end up in the queue in the right order.
Other ways to iterate backwards are:
LinkedList
's descending iteratorCollections.reverse(..)
Pick one which fits your case.