import static java.lang.System.*;  // cf. Java 5
import java.io.*;
import java.util.*;

/**
  * Classe 'tab_hachage' :  Test de qq. méthodes du conteneur 'HashMap'
  * @author   (c) ~/2A env.
  * @version  1.1   2006.10.10
  * @since    1.0   2005.10.01
  */
class tab_hachage   { 

   private    HashMap<String, Vector<String>>    hash;  // cf. Java 5

   public tab_hachage() { hash= new HashMap<String, Vector<String>>(); }

   public void insere(String author, String novel)  { 
      Vector<String> livres = hash.get(author);
      if    (livres == null)   { 
         // l'auteur 'author' est nouveau ici <==> il n'est pas enregistré encore !
          Vector<String> v = new Vector<String>();
          hash.put(author, v);
          v.add(novel); 
      }  else  { 
        // le nom de l'auteur est déjà enregistré dans la table !
          Vector<String> titres = livres;
          if ( !titres.contains(novel) )  titres.add(novel);
          // on ajoute le nouveau roman 'novel' parmi les titres anciens
       }
   }

   public void affich()  { 
       Iterator it = hash.keySet().iterator();
       while(it.hasNext())  { 
           String str = (String) it.next();
           out.println(str+" ==> ");
           Vector<String> titres = (Vector<String>) hash.get(str);
           for (int i=0; i<titres.size(); ++i)   { 
                   String s = (String) titres.elementAt(i);
                   out.println("                       "+s);  
           }
       }
    }
}

/**
  * Classe 'tab_hach' :  pour tester la classe 'tab_hachage'
  * @author   (c) ~/2A env.
  * @version  1.1   2006.10.10
  * @since    1.0   2005.10.01
  * @see      tab_hachage
  */

public class tab_hach   {
   public static void main (String [] arg)  {  
       out.println("\ntab_hach : Un exemple d'emploi de HashMap en Java 5");
       out.println("\t(c)~/2a env. 2006.10.10  09h30 \n");
       tab_hachage th = new tab_hachage();
       th.insere("Steinbeck","Les Raisins de la col\u00e8re");
       th.insere("Hugo","Les Mis\u00e9rables");
       th.insere("Duras","Un barrage contre le Pacifique");
       th.insere("Duras","L'Amant");
       th.insere("Zola","Germinal");     
       th.insere("Zola","La B\u00eate humaine");
       th.affich();
       es.attente();
  }
}
