TreeSet:
Data elements are sortable and non-repeatable.
When using TreeSet,
Either the added element already implements the Comparable interface, otherwise in
When reeSet.add(bean) is executed, an exception of cannot be cast to java.lang.Comparable will be reported
Either give TreeSet (Comparator class)
TreeMap is the key that needs to implement the Comparable interface
or new TreeMap (passing the Comparator class)
The TreeSet class uses:
public class Person { private final String name; // name private final int handsome;//handsome index public Person() { name =null; handsome =0; } public Person(String name, int handsome) { super(); this.name = name; this.handsome = handsome; } public String getName() { return name; } public int getHandsome() { return handsome; } @Override public String toString() { return "Name:"+this.name+",handsome index:"+this.handsome+"\n"; } } Use new HashSet (Comparator method) public static void main(String[] args) { Person p1 =new Person("您",100); Person p2 =new Person("Andy Lau",1000); Person p3 =new Person("Leung Chiu Wai",1200); Person p4 =new Person("Old Pei",50); / / Stored in the TreeSet container in turn, using the sorted business class (anonymous inner class) TreeSet<Person> persons =new TreeSet<Person>( new java.util.Comparator<Person>(){ @Override public int compare(Person o1, Person o2) { return -(o1.getHandsome()-o2.getHandsome()); } } ); persons.add(p1); //TreeSet sorts all previous data when adding data persons.add(p2); persons.add(p3); persons.add(p4); System.out.println(persons); /* //change data p4.setHandsome(100); p4.setName("you"); */ //p4 and p1 are duplicated System.out.println(persons); }
After the entity class implements the Comparable interface, it is added to the TreeSet for automatic sorting:
public class Worker implements java.lang.Comparable<Worker> { // type of work private String type; //salary private double salary; public Worker() { // TODO Auto-generated constructor stub } public Worker(String type, double salary) { super(); this.type = type; this.salary = salary; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } /** *In ascending order of salary */ @Override public int compareTo(Worker o) { return this.salary>o.salary?1:( this.salary==o.salary?0:-1); } @Override public String toString() { return "Type of work:"+this.type+",salary:"+this.salary+"\n"; } } public class TreeSetDemo2 { /** * @param args */ public static void main(String[] args) { Worker w1 =new Worker("garbage collector",12000); Worker w2 =new Worker("Migrant Worker",8000); Worker w3 =new Worker("Programer",5000); TreeSet<Worker> employees =new TreeSet<Worker>(); employees.add(w1); employees.add(w2); employees.add(w3); System.out.println(employees); } }
TreeMap uses:
new TreeMap(Comparator) method: public static void main(String[] args) { Person p1 =new Person("您",100); Person p2 =new Person("Andy Lau",1000); Person p3 =new Person("Leung Chiu Wai",1200); Person p4 =new Person("Old Pei",50); TreeMap<Person,String> map =new TreeMap<Person,String>(new java.util.Comparator<Person>(){ @Override public int compare(Person o1, Person o2) { return -(o1.getHandsome()-o2.getHandsome()); } } ); map.put(p1, "bjsxt"); map.put(p2, "bjsxt"); map.put(p3, "bjsxt"); map.put(p4, "bjsxt"); // view key Set<Person> persons =map.keySet(); System.out.println(persons); }
TreeMap uses the KEY is the Comparable interface class to achieve automatic sorting of added entities: public static void main(String[] args) { Worker w1 =new Worker("garbage collector",12000); Worker w2 =new Worker("Migrant Worker",8000); Worker w3 =new Worker("Programer",5000); TreeMap<Worker,String > employees =new TreeMap<Worker,String >(); employees.put(w1,"hellworld"); employees.put(w2,"hellworld"); employees.put(w3,"hellworld"); System.out.println(employees.keySet()); }