Java 如何将列表转换为队列以实现先进先出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23485944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert list to queue to achieve FIFO
提问by user3318622
public List<Place> getAllInactivePlaces() {
return this.placeDAO.findAllUnApprovedList();
}
Hi the method i shown above is to find all places.What i need is ,i want to convert Queue to get FIFO something like this-----
嗨,我上面显示的方法是找到所有地方。我需要的是,我想转换队列以获得像这样的 FIFO -----
Queue<Place>placeQueue=getAllInactivePlaces();
采纳答案by Aurand
Assuming that you currently have a list which is not a queue (like an ArrayList
). Further assuming that you cannot pick a data structure appropriate to your use-case to start with:
假设您当前有一个不是队列的列表(如ArrayList
)。进一步假设您无法选择适合您的用例的数据结构来开始:
Queue<Place> queue = new LinkedList<>(yourList);
回答by Thilo
"Achieve FIFO" is less a question of the data structure being used, but more about the code that puts in/pulls out data from that structure.
“实现 FIFO”不是正在使用的数据结构的问题,而是更多关于从该结构放入/取出数据的代码。
You need to update that calling code to work with the Queue interfaceinstead of a List.
您需要更新该调用代码以使用Queue 接口而不是 List。