Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序

package TreeSetTest;

import java.util.Iterator;  
import java.util.TreeSet;

import javax.management.RuntimeErrorException;

/*
可以对set集合中的元素进行排序,其底层的数据结构是二叉树,
保证元素唯一性的依据是compareTo和return 0;

TreeSet排序的第一种方式
让元素自身具备比较性
元素需要实现Comparable接口,覆盖compareTo方法
这种方式也成为元素的自然顺序,或者叫做默认顺序

TreeSet排序的第二种方式
当元素自身不具备比较性时,或者具备的比较性不是所需的
这时就需要让集合自身具备比较性
在集合初始化时,就有了比较方式

记住:当比较时,如果主要条件相同,就比较次要条件
*/
 
class Student implements Comparable  // 该接口强制让对象具有比较性
{
	private String name;
	private int age;
	
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
	public int compareTo(Object obj)
	{
		/*
		
		if (!(obj instanceof Student))
		{
			throw new RuntimeException("不是学生对象");
		}
		Student s = (Student)obj;
		// System.out.println(this.name+"=====Compare====="+this.age);
		if (this.age >s.age)
		{
			return 1;
		}
		else if (this.age == s.age) {
			// 如果年龄相同比较姓名
			return this.name.compareTo(s.name);
		}
		else {
			return -1;
		}
		
		*/
		return -1;
	}
	
	public String getname()
	{
		return this.name;
	}
	
	public int getage()
	{
		return age;
	}
}

public class TreeSetDemo
{
	public static void main(String[] args)
	{
		TreeSet<Student> tsSet =new TreeSet<Student>();
		
		tsSet.add(new Student("as", 12));
		tsSet.add(new Student("fvhgj",45));
		tsSet.add(new Student("ert", 15));
		tsSet.add(new Student("wenjia", 31));
		tsSet.add(new Student("wejia", 1));
		
		Iterator<Student> iterator = tsSet.iterator();
		
		while (iterator.hasNext())
		{
			Student stu = (Student)iterator.next();
			System.out.println(stu.getage()+"======"+stu.getname());
		}
	}
}


////////////////////////////////////////////
////////////////////////////////////////////
package TreeSetTest;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


/*
	当元素自身不具备比较性,或者具备的比较性不是所需要的。
	这时需要让容器自身具备比较性。
	定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
	
	当两种排序都存在时,一比较器为主
	
	定义一个类,实现Comparator接口,覆盖compare方法
	二叉树都是以 return 0;判断元素是否相等
*/
 
public class TreeSetDemoTwo
{
	public static void main(String[] args)
	{
		TreeSet<Student> tsSet = new TreeSet<Student>(new MyCompare());
		
		tsSet.add(new Student("wenjai10", 29));
		tsSet.add(new Student("wenjai11", 28));
		tsSet.add(new Student("wenjai12", 27));
		tsSet.add(new Student("wenjai13", 26));
		tsSet.add(new Student("wenjai11", 21));
		
		Iterator<Student> it = tsSet.iterator();
		while (it.hasNext())
		{
			Student student = (Student) it.next();
			System.out.println(student.getname()+"======"+student.getage());
			
		}
	}
}

class MyCompare implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		Student s1 = (Student)o1;
		Student s2 = (Student)o2;
		
		int num = s1.getname().compareTo(s2.getname());
		if (num == 0)
		{
			// 由于整数类型也有比较方法,所以将整数封装成对象
			return new Integer(s1.getage()).compareTo(new Integer(s2.getage()));
			/*
			if (s1.getage()>s2.getage())
			{
				return 1;
			}
			if (s1.getage() == s2.getage())
			{
				return 0;
			}
			return -1;
			*/
		}
		return num;
	}
}

///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
package TreeSetTest;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
练习: 按照字符串长度排序

字符串本身具有比较性,但是它的比较方式不是所需的

这时就只能用比较器
*/
 
public class TreeSetPractice_One
{

	public static void main(String[] args)
	{
		TreeSet<String> ts = new TreeSet<String>(new stringLenCompare());
		
		ts.add("asd");
		ts.add("bb");
		ts.add("ba");
		ts.add("ityior");
		ts.add("hysdhbgg");
		
		Iterator<String> it = ts.iterator();
		for (String string : ts)
		{
			System.out.println(string);
		}
	}
}

class stringLenCompare implements Comparator
{
	@Override
	public int compare(Object o1, Object o2)
	{
		// TODO Auto-generated method stub
		String str1 = (String)o1;
		String str2 = (String)o2;
		
		/*
		if (str1.length()>str2.length())
		{
			return 1;
		} 
		if(str1.length() == str2.length())
		{
			return 0;
		}
		*/
		int num = new Integer(str1.length()).compareTo(new Integer(str2.length()));
		if (num == 0)
		{
			return str1.compareTo(str2);
		}
		return num;
		// return 0;
	}
}

  

posted @ 2015-07-17 10:12  JiangXiaoKun  阅读(668)  评论(0编辑  收藏  举报