码迷,mamicode.com
首页 > 编程语言 > 详细

集合TreeSet(自然排序与定制排序)

时间:2015-06-15 11:21:58      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:treeset   集合   

一、TreeSet的自然排序:

步骤:让元素自身具备比较性,

实现Compareable接口,覆盖其CompareTo方法

 

class Student implements Comparable//第一:实现Compareable接口

{

    private String name;

    private int age;

 

    Student(String name,int age)

    {

       this.name = name;

       this.age = age;

    }

 

    public int compareTo(Object obj) //第二:复写CompareTo方法

    {

 

       //return 0;

      

       if(!(obj instanceof Student))   //第三:判断对象是否是特定类的一个实例

           throw new RuntimeException("不是学生对象");

       Student s = (Student)obj;

 

       System.out.println(this.name+"....compareto....."+s.name);

//第四:当前对象的年龄与插入对象的年龄进行比较,当前年龄大于插入对象的年龄时,返回1,

此时将插入二叉树的右边,当等于时,返回0,进行次要条件的比较,再次调用;当小于时,返回-1;

 

       if(this.age>s.age)  

           return 1;

       if(this.age==s.age)

       {

           return this.name.compareTo(s.name);

       }

       return -1;

       /**/

    }

 

    public String getName()

    {

       return name;

 

    }

    public int getAge()

    {

       return age;

    }

}

 

 

class TreeSetDemo

{

    public static void main(String[] args)

    {

       TreeSet ts = new TreeSet();

 

       ts.add(new Student("lisi02",22));

       ts.add(new Student("lisi007",20));

       ts.add(new Student("lisi09",19));

       ts.add(new Student("lisi08",19));

      

 

       Iterator it = ts.iterator();

       while(it.hasNext())

       {

           Student stu = (Student)it.next();

           System.out.println(stu.getName()+"..."+stu.getAge());

       }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二、TreeSet的定制排序

1、由来:当元素自身不具备比较性时,或者具备的比较性不是所需要的。

             这时就要让集合自身具备比较性,在初始化时,就有了比较方式。

2、步骤:1.实现comparator接口

            2.复写compare方法

            3.在创建TreeSet集合对象时,提供一个一个Comparator对象,


 

import java.util.Comparator;

import java.util.Set;

import java.util.TreeSet;

 

class Student1{

    private Integer age;

   

    public Student1(Integer age) {

       super();

       this.age = age;

    }

 

    public Integer getAge() {

       return age;

    }

 

    public void setAge(Integer age) {

       this.age = age;

    }

 

    @Override

    public String toString() {

       return age + "";

    }

}

 

class MyComparator implements Comparator{  //第一步:实现Comparator接口

   

    @Override

    public int compare(Object o1, Object o2) {   //第二步:实现一个campare方法

判断对象是否是特定类的一个实例

       if(o1 instanceof Student1 & o2 instanceof Student1){

           Student1 s1 =(Student1)o1;

           Student1 s2 =(Student1)o2;

           if(s1.getAge() > s2.getAge()){

              return -1;

           }else if(s1.getAge() < s2.getAge()){

              return 1;

           }

       }

       return 0;

    }

}

 

public class Demo15 {

    public static void main(String[] args) {

       Set<Student1> s= new TreeSet(new MyComparator());//第三步:创建TreeSet集合对象时,提供一个一个Comparator对象,

       /**

        * 要实现定制排序,需要在创建TreeSet集合对象时,提供一个一个Comparator对象,

        * 该对象里负责集合元素的排序逻辑;

        */

       s.add(new Student1(140));

       s.add(new Student1(15));

       s.add(new Student1(11));

       s.add(new Student1(63));

       s.add(new Student1(96));

      

       System.out.println(s);

    }

}

集合TreeSet(自然排序与定制排序)

标签:treeset   集合   

原文地址:http://blog.csdn.net/liuchuangjin/article/details/46500283

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!