码迷,mamicode.com
首页 > 其他好文 > 详细

TreeSet排序,存储自定义对象,自定义比较器示例

时间:2014-07-21 09:16:34      阅读:1526      评论:0      收藏:0      [点我收藏+]

标签:style   java   color   使用   strong   os   

Set:无序,不可以重复元素。
|--HashSet:数据结构是哈希表。线程是非同步的。
保证元素唯一性的原理:判断元素的hashCode值是否相同。
如果相同,还会继续判断元素的equals方法,是否为true。

|--TreeSet:可以对Set集合中的元素进行排序。
底层数据结构是二叉树。
保证元素唯一性的依据:compareTo方法return 0.

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

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

在集合初始化时,就有了比较方式。


示例:需求:
往TreeSet集合中存储自定义对象学生,按照学生的年龄进行排序

package tan;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
	public static void main(String[] args) {
		TreeSet ts=new TreeSet();
				ts.add(new Student("tan1", 21));
				ts.add(new Student("tan3", 20));
				ts.add(new Student("tan2", 23));
				ts.add(new Student("tan5", 21));
				
				Iterator it=ts.iterator();
				while(it.hasNext()){
				System.out.println(it.next());
				}
	}
}
class Student implements Comparable{
	private String name;
	private Integer age;
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		
		return "name:"+this.name+" "+"age:"+this.age;
	}
	@Override
	public int compareTo(Object obj) {
		if(!(obj instanceof Student))throw new RuntimeException("非学生对象");
		Student s=(Student)obj;
		if(this.age>s.age) return 1;
		//排序时,当主要条件相同时,一定判断一下次要条件。
		if(this.age==s.age)
		{
			return this.name.compareTo(s.name);
		}
		return -1;
	}
	
}

自定义比较器

package tan;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
	public static void main(String[] args) {
		//在这里面可以传入自定义比较器
		TreeSet ts=new TreeSet(new StudentAgeComparator());
		
				ts.add(new Student("tan01", 21));
				ts.add(new Student("tan03", 20));
				ts.add(new Student("tan03", 22));
				ts.add(new Student("tan0012", 23));
				ts.add(new Student("tan007", 21));
				
				Iterator it=ts.iterator();
				while(it.hasNext()){
				System.out.println(it.next());
				}
	}
}
class Student implements Comparable{
	private String name;
	private Integer age;
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		
		return "name:"+this.name+" "+"age:"+this.age;
	}
	@Override
	public int compareTo(Object obj) {
		if(!(obj instanceof Student))throw new RuntimeException("非学生对象");
		Student s=(Student)obj;
		if(this.age>s.age) return 1;
		//排序时,当主要条件相同时,一定判断一下次要条件。
		if(this.age==s.age)
		{
			return this.name.compareTo(s.name);
		}
		return -1;
	}
	
}
//自定义姓名比较器
class StudentNameComparator implements Comparator{

	@Override
	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){
			//因为Ingteger已经实现了comparable接口
			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;
	}
	
}
//自定义年龄比较器
class StudentAgeComparator implements Comparator<Student>{

	@Override
	public int compare(Student o1, Student o2) {
		int i=o1.getAge()-o2.getAge();
		return i;
	}	
}

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


字符串本身具备比较性,但是它的比较方式不是所需要的,这时就只能使用比较器。

package tan;
import java.util.*;
public class TreeSetTest {
	public static void main(String[] args) {
		TreeSet ts = new TreeSet(new StrLengthComparator());

		ts.add("abcd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("z");
		ts.add("hahaha");

		Iterator it = ts.iterator();

		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

class StrLengthComparator implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {
		String s1 = (String) o1;
		String s2 = (String) o2;
		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
		//当第一条件满足时,判断第二个条件按照自然顺序排序
		if(num==0){
			return s1.compareTo(s2);
		}
		return num;
	}
}


TreeSet排序,存储自定义对象,自定义比较器示例,布布扣,bubuko.com

TreeSet排序,存储自定义对象,自定义比较器示例

标签:style   java   color   使用   strong   os   

原文地址:http://blog.csdn.net/u010834071/article/details/37990491

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