开发者

What's the Java equivalent of C++'s STL Queue?

I was browsing through the Java docs to look for the Java equivalent for C++'s STL Queue, but all I found was an interface called Queue and a bunch of implementations I can't make heads or tails of.

Does Java have an implementation for Queue that's just a FIFO data structure without the add开发者_运维技巧ed bells and whistles? I only need the enqueue, dequeue and front operations and the data structure should allow duplicates.


Queue will work. Use any implementation you like. LinkedList or ConcurrentLinkedQueue for example.

enqueue = offer(..)
dequeue = poll()
front = peek()


That docs page lists all the classes that implement the interface. So, for instance, you can do the following (DISCLAIMER: hasn't been near a compiler):

Queue<E> q = new LinkedList<E>();

E x1 = new E();
E x2 = new E();
E x3;

q.offer(x1);
q.offer(x2);

x3 = q.poll();


You can just use LinkedList. Sure, it has lots of functionality you don't need, but it's not hurting you either.


The java.util.LinkedList class is probably what you want, and the methods would be "add", "remove", and "element".


What you're probably looking for is a double-ended queue. See the Deque interface and it's implementing classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜