TreeSet集合中储存学生对象,按照其年龄进行排序
 
 
 
 
TreeSet对元素进行排序的方式一:   
让元素自身具备比较功能,元素就需要实现Comparable接口,覆盖compareTo方法。
TreeSet对元素进行第二种排序方式:
    让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。
    将该类对象作为参数传递给TreeSet集合的构造函数。

方式一实现:
    public class PersonBean implements Comparable {    
        
        private String name;    
        private int age;    
        
        PersonBean(String name, int age) {    
            this.name = name;    
            this.age = age;    
        }    
        
        public String getName() {    
            return name;    
        }    
        
        public int getAge() {    
            return age;    
        }    
        
        @Override    
        public int compareTo(Object obj) {    
            if (!(obj instanceof PersonBean))    
                throw new RuntimeException();    
            /**  
             * 在这里定义比较对象大小的方法  
             * 先进行年龄的比较,年龄不相等再进行姓名的比较  
             * 如果年龄与姓名都相等,那么就代表同一个对象  
             */    
            PersonBean pe = (PersonBean) obj;    
            if (this.age > pe.age)    
                return 1;    
            if (this.age == pe.age) {    
                return this.name.compareTo(pe.name);    
            }    
        
            return -1;    
        }    
    }   

public class ListDemoClass {    
    
    public static void main(String[] args) {    
    
        // 向这个集合中存储自定义对象    
        TreeSet ts = new TreeSet();    
        ts.add(new PersonBean("lishi", 22));    
        ts.add(new PersonBean("lishi1", 23));    
        ts.add(new PersonBean("lishi3", 25));    
        ts.add(new PersonBean("lishi4", 25));    
    
        //获取迭代器,取出其中的元素    
        Iterator it = ts.iterator();    
        while (it.hasNext()) {    
            PersonBean pe = (PersonBean) it.next();    
            System.out.println(pe.getAge() + "  " + pe.getName());    
        }    
    
    

java基础—TreeSet集合中储存自定义对象(java集合二)_Java教程

第二种方式:

自定义一个比较器
import java.util.Comparator;    
import java.util.HashSet;    
import java.util.Iterator;    
import java.util.TreeSet;    
    
class CustomCompare implements Comparator {    
    public int compare(Object obj1, Object obj2) {    
        //获取将要进行比较的两个对象    
        PersonBean p1 = (PersonBean) obj1;    
        PersonBean p2 = (PersonBean) obj2;    
        //先进行比较两个对象中的name是否相等,如果相等,那么将返回0    
        int num = p1.getName().compareTo(p2.getName());    
        //如果name相等,那么再比较age的大小     
        if (num == 0) {    
            if (p1.getAge() > p2.getAge())    
                return 1;    
            if (p1.getAge() == p2.getAge())    
                return 0;    
            return -1;    
        }    
        return num;    
    }    
} 

public class ListDemoClass {    
    
    public static void main(String[] args) {    
    
        // 向这个集合中存储自定义对象    
        TreeSet ts = new TreeSet(new CustomCompare());    
        ts.add(new PersonBean("lishi", 22));    
        ts.add(new PersonBean("lishi1", 23));    
        ts.add(new PersonBean("lishi3", 25));    
        ts.add(new PersonBean("lishi3", 26));    
        ts.add(new PersonBean("lishi3", 24));    
        ts.add(new PersonBean("lishi4", 25));    
    
        // 取出集合中的元素数据,并将相关信息输出在控制台上    
        Iterator it = ts.iterator();    
        while (it.hasNext()) {    
            PersonBean pe = (PersonBean) it.next();    
            System.out.println(pe.getAge() + "  " + pe.getName());    
        }    
    
    } 

java基础—TreeSet集合中储存自定义对象(java集合二)_Java开发_02

当然,这里只是说明了两种思想,比如在使用TreeSet进行String类型数据存储的时候,集合会将元素以其自然顺序进行排序,若要按照字符串的长度为规则进行储存,则需要定义一个比较器

 

详细文章请查看: 点击打开链接