The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util  [50 examples] > Collections  [7 examples]

e343. Implementing a Stack

    LinkedList stack = new LinkedList();
    
    // Push on top of stack
    stack.addFirst(object);
    
    // Pop off top of stack
    Object o = stack.getFirst();
    
    // If the queue is to be used by multiple threads,
    // the queue must be wrapped with code to synchronize the methods
    stack = (LinkedList)Collections.synchronizedList(stack);

 Related Examples
e342. Implementing a Queue
e344. Implementing a Least-Recently-Used (LRU) Cache
e345. Listing the Elements of a Collection
e346. Storing Primitive Types in a Collection
e347. Creating a Copy of a Collection
e348. Making a Collection Read-Only

See also: Arrays    Bits    Dates    Hash Tables    Lists    Property Files    Sets    Sorted Collections    Time    Timers   


© 2002 Addison-Wesley.