TreeSet排序,存储自己定义对象,自己定义比較器演示样例

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;
}
}
上一篇:微信H5页面登录到支付的心得


下一篇:判断是否微信浏览器,获取cookie,获取URL来源等