La collection Stack constitue une pile du type dernier entré/premier sorti (LIFO : Last-In-First-Out).

La classe Stack est une extension de Vector.

Un seul constructeur permet d'instancier la classe Stack. Lorsque l'objet est créé, il ne possède aucun élément.

Stack pile = new Stack();

La classe Stack possède cinq méthodes supplémentaires permettant d'assurer un comportement d'une pile à tout objet créé à partir de cette classe.

Les opérations d'entrée et de sortie de la pile sont exécutées respectivement par les méthodes push() et pop().

// récupère l'objet au sommet de la pile
Object element = pile.pop();
// ajoute l'objet spécifié au sommet de la pile et le retourne
Object element = pile.push(objet);

Une exception EmptyStackException est levée si l'objet Stack est vide lors de ces opérations.

La méthode peek() est chargée de retourner l'objet, sans le supprimer, situé au sommet de la pile.

Object element = pile.peek();

La position d'un objet, par rapport au sommet de la liste, peut être récupérée par le biais de la méthode search(). Si l'objet recherché n'est pas contenu dans la liste, alors la méthode retourne -1.

int position = pile.search();

Enfin, la méthode empty() teste si la pile ne contient aucun élément.

boolean resultat = pile.empty();

Toutes les opérations possibles sur les vecteurs sont également réalisables sur les objets Stack. Néanmoins, son fonctionnement doit répondre aux exigences d'une pile.

Exemple [voir]
import java.util.Stack;

public class programme {
  private static Stack pile = new Stack();
  private static int inc = 1;
  public static void main(String[] args) {
    ajout(new Video("Voyage au bout de l'enfer", "Michael Cimino", 1978));
    ajout(new Video("Le jour le plus long", "Ken Annakin", 1962));
    ajout(new Video("Un pont trop loin", "Richard Attenborough", 1977));
    ajout(new Video("Platoon", "Oliver Stone", 1986));
    ajout(new Video("Full metal jacket", "Stanley Kubrik", 1987));
    ajout(new Video("La ligne rouge", "Terrence Malick", 1998));
    ajout(new Video("The patriot", "Roland Emmerich", 2000));
    ajout(new Video("Outrages", "Brian De Palma", 1990));
    ajout(new Video("Paris brûle-t-il ?", "René Clément", 1966));
    ajout(new Video("Tora ! Tora ! Tora !", "Richard Fleischer", 1970));
    ajout(new Video("Guerre et Paix", "King Vidor", 1956));
    ajout(new Video("Apocalypse now", "Francis Ford Coppola", 1979));
    System.out.println("Taille après ajouts = " + pile.size());
    inc = 1;
    while(!pile.empty())
      System.out.println("Extraction n°" + inc++ + "\t: " + pile.pop());
    System.out.println("Taille après extraction = " + pile.size());
  }
  public static void ajout(Video v){
    pile.push(v);
    System.out.println("Ajout n°" + inc++ + "\t: " + pile.peek());
  }
}

// Classe Video
import java.util.GregorianCalendar;
import java.util.Calendar;

public class Video implements Comparable {
  private String titre, realisateur;
  private int annee;
  
  public Video(){
    this("Inconnu", "Inconnu", 0);
  }
    public Video (String[] infos) {
      this(infos[0], infos[1], Integer.parseInt(infos[3]));
    }
    public Video (String titre, String realisateur){
      this(titre, realisateur, (new GregorianCalendar()).get(Calendar.YEAR));
    }
  public Video (String titre, String realisateur, String annee){
    this(titre, realisateur, Integer.parseInt(annee));
  }
  public Video (String titre, String realisateur, int annee){
    if (titre == null || realisateur == null)
      throw new NullPointerException();
    this.titre = titre;
    this.realisateur = realisateur;
    this.annee = annee;
  }
  
  public String obtenirTitre(){
    return this.titre;
  }
  public String obtenirRealisateur(){
    return this.realisateur;
  }
  public int obtenirAnnee(){
    return this.annee;
  }

  public void fournirTitre(String titre){
    this.titre = titre;
  }
  public void fournirRealisateur(String realisateur){
    this.realisateur = realisateur;
  }
  public void fournirAnnee(int annee){
    this.annee = annee;
  }
  
  public int hashCode(){
    return annee * titre.hashCode() + realisateur.hashCode();
  }
  
  public boolean equals(Object o){
    if(!(o instanceof Video))
      return false;
    Video v = (Video)o;
    if(this.hashCode() == v.hashCode())
      return true;
    return false;
  }
  
  public int compareTo(Object o){
    if(!(o instanceof Video))
      throw new ClassCastException();
    Video v = (Video)o;
    int comparaison;
    if((comparaison = titre.compareTo(v.obtenirTitre())) != 0)
      return  comparaison;
    else if((comparaison = realisateur.compareTo(v.obtenirRealisateur())) != 0)
      return comparaison;
    else
      return (new Integer(annee)).compareTo(new Integer(v.obtenirAnnee()));
  }
  
  public String toString(){
    StringBuffer res = new StringBuffer("[");
    res.append(titre);
    res.append(", ");
    res.append(realisateur);
    res.append(", ");
    res.append(annee);
    return res.append("]").toString();
  }
}