import java.util.*; public class Test1{ public static void main(String a[]){ Set s = new TreeSet(); s.add(new Person(20)); s.add(new Person(10)); System.out.println(s); } } class Person{ Person(int i){} }
TreeSet is a container which put objects in sorted order. In order to sort data , object being added should be of type Comparable . In class Person implements Comparable interface and implements its method. When you add any new object , this method will be called for comparison .
This code will compile and run without exceptions:
import java.util.*; public class Test1{ public static void main(String a[]){ Set s = new HashSet(); <--- guess why s.add(new Person(20)); s.add(new Person(10)); System.out.println(s); } } class Person{ Person(int i){} } [ November 13, 2007: Message edited by: Andry Dub ]
Java is my love
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
a bit of art, as a gift, the permaculture playing cards