Académique Documents
Professionnel Documents
Culture Documents
Page 1
Les collections Java
u Des interfaces
«interface»
«interface» «interface»
Collection
Iterator utilise Map
utilise
LinkedList
«interface»
Vector
SortedSet TreeMap
ArrayList
HashSet
Stack
TreeSet
n Une collection est utilisée pour manipuler n’importe quelle collection réelle à
partir d’une interface de services générale :
n C’est une collection ordonnée (parfois appelée Séquence) qui peut contenir des
éléments dupliqués (redondance).
«interface» «interface»
AbstractCollection
Iterator Collection
utilise
ArrayList
import java.util.* ;
class Demo
{
public static void main (String args [ ])
{
ArrayList list ;
list = new ArrayList () ;
list.add ( new Point ( 10 , 20 ) ) ;
list.add ( new Point ( 20 , 40 ) ) ;
list.add ( new Point ( 30 , 50 ) ) ;
for ( int i = 0 ; i < list.size () ; i++ )
( ( Point ) list.get ( i ) ).print () ;
}
}
import java.util.* ;
class Demo
{ public static void main (String args [ ])
{ ArrayList list = new ArrayList () ; ;
Point p1 = new Point ( 10 , 20 ) ;
list.add ( p1 ) ;
list.add ( new Point ( 20 , 40 ) ) ;
list.add ( new Point ( 30 , 50 ) ) ;
if ( list.contains ( p1 ) )
System.out.println ( "ok" ) ;
else
System.out.println ( "non ok " ) ;
if ( list.contains ( new Point ( 20 , 40 ) ) ) System.out.println ( "ok" ) ;
else System.out.println ( "non ok " ) ;
}
}
class Point
{
…..
public boolean equals ( Object obj )
{
if ( ! ( obj instanceof Point ) )
return false ;
return ( ( x == ( ( Point ) obj ).x ) && ( y == ( ( Point ) obj ).y ) ) ;
}
}
u vérifier s'il reste encore d'autres objets dans la séquence via la méthode hasNext().
import java.util.* ;
class Demo
{
public static void main ( String args [ ] )
{
ArrayList list ;
list = new ArrayList () ;
list.add ( "chaine1" ) ;
list.add ( "chaine2" ) ;
list.add ( "chaine3" ) ;
for ( Iterator i = list.iterator () ; i.hasNext ( ) ; )
System.out.println ( i.next() ) ;
}
}
n Elle correspond à un groupe d’objets qui n’accepte pas 2 objets égaux au sens de equals
(comme les ensembles des mathématiques).
n L’interface Set n’ajoute aucune méthode à l’interface Collection. Elle ne spécifie que les
restrictions attendues sur les méthodes de base (unicité des objets).
n La méthode add n’ajoute pas un élément si un élément égal est déjà dans l’ensemble (la
méthode renvoie alors false)
import java.util.* ;
public class Demo
{
public static void main ( String args[] )
{
Set s = new HashSet ( );
String tab[] = { "a" , "b" , "c" , "b" } ;
for ( int i = 0 ; i < tab.length ; i++ )
if ( ! s.add ( tab[i] ) )
System.out.println ("duplication detectee : " + tab[i] );
System.out.println ( s.size() + " mots dans le set " + s );
}
} copyright ©Thierry Douchet
15
Janvier 2006
La classe TreeSet
n C’est un ensemble trié. Il garantit que les éléments sont rangés dans leur ordre
naturel.
n Une relation d’ordre sur des objets peut être définie en implémentant l’interface
Comparable
import java.util.* ;
n Exemple
import java.util.* ;
public class Demo
{
public static void main ( String args[] )
{
HashMap listePersonne = new HashMap () ;
listePersonne.put( "Bond" , new Personne ( "Bond" , "James" , 45) ) ;
listePersonne.put( "Jones" , new Personne ( "Jones" , "Indiana" , 40 )) ;
(( Personne )listePersonne.get ( "Bond" )).afficher() ;
}
}