Java Collections (LIFO Structure)

前端 未结 6 1398
栀梦
栀梦 2020-12-05 06:55

I am looking in the Collections framework of Java for a LIFO Structure (Stack) without any success. Basically I want a really simple stack; my perfect option would be a Dequ

相关标签:
6条回答
  • 2020-12-05 07:10

    I realize I'm late to the party here, but java.util.Collections (Java 7) has a static 'asLifoQueue' that takes a Deque argument and returns (obviously) a LIFO queue view of the deque. I'm not sure what version this was added.

    http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#asLifoQueue(java.util.Deque)

    0 讨论(0)
  • 2020-12-05 07:14

    There's actually a Stack class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html

    If you don't want to use that, the LinkedList class (http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html) has addFirst and addLast and removeFirst and removeLast methods, making it perfect for use as a stack or queue class.

    0 讨论(0)
  • 2020-12-05 07:16

    Deque, ArrayDeque, & LinkedList

    While this was asked a while ago it might be wise to provide a JDK6+ answer which now provides a Deque (deck) interface which is implemented by the ArrayDeque data structure and the LinkedList was updated to implement this interface.

    ConcurrentLinkedDeque & LinkedBlockingDeque

    Specialised forms for concurrent access also exist and are implemented by ConcurrentLinkedDeque and LinkedBlockingDeque.

    LIFO versus FIFO

    The one thing that is great about a deque is that it provides both LIFO (stack) and FIFO (queue) support it can cause confusion as to which methods are for queue operations and which are for stack operations for newcomers.

    IMHO the JDK should have a Stack interface and a Queue interface that could still be implemented by the likes of ArrayDeque but only expose the subset of methods required for that structure, i.e. a LIFO could define pop(), push() and peek(), then in the context of

    LIFO<String> stack = new ArrayDeque<>();
    

    only stack operations are exposed which stops someone accidentally calling add(E) when push(E) was intended.

    0 讨论(0)
  • 2020-12-05 07:18

    Stack class is slowly: methods are synchronized + Stack extends synchronized Vector

    0 讨论(0)
  • 2020-12-05 07:26

    There is a Stack class in the API. Will this meet your needs?

    0 讨论(0)
  • 2020-12-05 07:30

    Deque & LinkedList

    Just for the sake of completeness I'm providing an example using Deque interface and a LinkedList implementation.

        Deque<String> deque = new LinkedList<>();
        deque.add("first");
        deque.add("last");
    
        // returns "last" without removing it
        System.out.println(deque.peekLast());
    
        // removes and returns "last"
        System.out.println(deque.pollLast());
    

    Backing up a Deque by a LinkedList is great for performance, since inserting and removing elements from it is done in constant time (O(1)).

    Using a LinkedList alone:

        LinkedList<String> list = new LinkedList<>();
        list.add("first");
        list.add("last");
    
        // returns "last" without removing it
        System.out.println(list.getLast());
    
        // removes and returns "last"
        System.out.println(list.removeLast());
    
    0 讨论(0)
提交回复
热议问题