简体   繁体   中英

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 added 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.

java.util.LinkedList类可能就是你想要的,方法是“add”,“remove”和“element”。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM