java Treesets and code example

Example 1: TreeSet

// par défaut
TreeSet ensemble = new TreeSet();
// comparateur
Comparator comparateur = new Comparateur();
TreeSet ensemble = new TreeSet(comparateur);

// classe Comparateur implémentant l'interface Comparator
public class Comparateur implements Comparator {
  public int compare(Object obj1, Object obj2){
    return ((Comparable)obj2).compareTo(obj1);
  }
  public boolean equals(Object obj){
    return this.equals(obj);
  }
}

Example 2: TreeSet

// extrait de la classe TreeSet
public class TreeSet extends AbstractSet
        implements SortedSet, Cloneable, java.io.Serializable {
  //...
  public TreeSet() {
    this(new TreeMap());
  }
  //...
}

Tags:

Java Example