- 第二种:在集合中定义排序 实现Comparator接口 覆盖compare方法。
-
- TreeSet(Comparator<? super E> comparator)
- 构造一个新的空 TreeSet,它根据指定比较器进行排序。
-
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
-
- public class TreeSetTest {
- public static void main(String[] args) {
- TreeSet treeset = new TreeSet( new mycomp());//定义一个集合
- treeset.add(new person2(10, "liuyia"));
- treeset.add(new person2(10, "liuyib"));
- treeset.add(new person2(15, "liuyi34"));
- treeset.add(new person2(11, "liuyi4"));
- treeset.add(new person2(12, "liuyi4"));
-
- Iterator itera = treeset.iterator();
- while (itera.hasNext()) {
- System.out.println(itera.next());
- }
-
- }
- }
-
- class person2 {
- private int age;
-
- private String name;
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public person2(int age, String name) {
- this.age = age;
- this.name = name;
- }
- public String toString()
- {
- return age+" = "+"name"+name;
- }
- }
-
-
- class mycomp implements Comparator
- {
-
- public int compare(Object o1, Object o2) {
- person2 p1 = (person2)o1;
- person2 p2 = (person2)o2;
- return -(p1.getAge()- p2.getAge());
- }
- }
- Treeset 的自定义的两种排序方式第一种:在元素中定义排序规则。元素自身具有比较性实现Comparable接口 覆盖compareTo方法import java.util.Iterator;import java.util.TreeSet;/*** *TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照 自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。 我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。 注意排序時:當主要的條件相同時,判斷次要條件。 * @author Administrator * */public class TreeSetTest { public static void main(String[] args) { TreeSet treeset = new TreeSet();//定义一个集合 treeset.add(new person2(10, "liuyia")); treeset.add(new person2(10, "liuyib")); treeset.add(new person2(15, "liuyi34")); treeset.add(new person2(11, "liuyi4")); treeset.add(new person2(12, "liuyi4")); Iterator itera = treeset.iterator(); while (itera.hasNext()) { System.out.println(itera.next()); } }}class person2 implements Comparable {//实现Comparable 接口 private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public person2(int age, String name) { this.age = age; this.name = name; } public int compareTo(Object o) { if(!(o instanceof person2)) throw new RuntimeException("對象不對哇!!"); person2 p = (person2)o; if(this.age>p.age) { return -1; } if(this.age<p.age) { return 1; } if(this.age==p.age) { return this.name.compareTo(p.name);//當主要的條件也就是age的值相同時時候此時判斷次要條件姓名 } return -1; } //用於設置打印結果 public String toString() { return age+" = "+"name"+name; }}第二种:在集合中定义排序 实现Comparator接口 覆盖compare方法。TreeSet(Comparator<? super E> comparator) 构造一个新的空 TreeSet,它根据指定比较器进行排序。import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;public class TreeSetTest { public static void main(String[] args) { TreeSet treeset = new TreeSet( new mycomp());//定义一个集合 treeset.add(new person2(10, "liuyia")); treeset.add(new person2(10, "liuyib")); treeset.add(new person2(15, "liuyi34")); treeset.add(new person2(11, "liuyi4")); treeset.add(new person2(12, "liuyi4")); Iterator itera = treeset.iterator(); while (itera.hasNext()) { System.out.println(itera.next()); } }}class person2 { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public person2(int age, String name) { this.age = age; this.name = name; } public String toString() { return age+" = "+"name"+name; }}class mycomp implements Comparator{ public int compare(Object o1, Object o2) { person2 p1 = (person2)o1; person2 p2 = (person2)o2; return -(p1.getAge()- p2.getAge()); }}
|
|