import java.util.*; class MinMaxAlgorithme { private Comparator comparator; private Object min,max; MinMaxAlgorithme() { } MinMaxAlgorithme( Comparator comparator ) { this.comparator = comparator; } void process( Collection c ) { Iterator it = c.iterator(); if (it.hasNext()) min = max = it.next(); while (it.hasNext()) { Object o = it.next(); if (comparator==null) { if (((Comparable)o).compareTo(min)<0) min = o; if (((Comparable)o).compareTo(max)>0) max = o; } else { if (comparator.compare( o, min )<0) min = o; if (comparator.compare( o, max )>0) max = o; } } } Object getMin() { return min; } Object getMax() { return max; } } public class Demo { public static void main( String args[] ) { Set set = new HashSet(); set.add (new Integer(1)); set.add (new Integer(10)); set.add (new Integer(100)); set.add (new Integer(9)); set.add (new Integer(99)); //-- Avec interface Comparable MinMaxAlgorithme num = new MinMaxAlgorithme(); num.process(set); System.out.println("MIN:" + num.getMin() + " MAX:" + num.getMax() ); //-- Avec interface Comparator MinMaxAlgorithme str = new MinMaxAlgorithme( new Comparator() { public int compare(Object o1, Object o2) { return o1.toString().compareTo( o2.toString() ); } } ); str.process(set); System.out.println("MIN:" + str.getMin() + " MAX:" + str.getMax() ); } }