source code TreeSet with comparator integer comparison
package myutil;
import java.util.Comparator;
import java.util.TreeSet;
public class ComparatorInteger {
public static void main(String arg[])
{
TreeSet t=new TreeSet(new MyCompare1());
t.add(new Integer(10));
t.add(new Integer(30));
t.add(new Integer(40));
System.out.println(t);
}
}
class MyCompare1 implements Comparator
{
public int compare(Object o1,Object o2)
{
Integer i11=(Integer)o1;
Integer i22=(Integer)o2;
int i1=i11.intValue();
int i2=i22.intValue();
if(i1 < i2)
return -1;
else
if(i1 > i2)
return +1;
else
return 0;
}
}
output:
[10, 30, 40]
0 comments:
Post a Comment