Set:无序,不可以重复元素。常用的子类,
HashSet:数据结构是哈希表。线程是非同步的。
保证元素唯一性的原理:判断元素的hashCode值是否相同。
如果相同,还会继续判断元素的equals方法,是否为true。
TreeSet:可以对Set集合中的元素进行排序。
底层数据结构是二叉树。
保证元素唯一性的依据:
compareTo方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性。
元素需要实现Comparable接口,覆盖compareTo方法。
也种方式也成为元素的自然顺序,或者叫做默认顺序。
TreeSet的第二种排序方式。
当元素自身不具备比较性时,或者具备的比较性不是所需要的。
这时就需要让集合自身具备比较性。
在集合初始化时,就有了比较方式。
下面就用Student类,将person对象存入到TreeSet中,并按照想要的方式经行排序:
将Student对象存入TreeSet中,第一种取出方式:
import java.util.Iterator;
import java.util.TreeSet;
public class Bolg3 {
/**
* @param args
*/
/*
* collection集合,中的TreeSet,可以按照自己想要的比较方法来排序,有两种比较方法,
* 现在分别用这两种方法来实现它;
*
* student 类有name属性和age属性,成绩属性,如果姓名和年纪都相同则为同一个人不存入;
* 按照age的大小,进行排序,若年纪相同就按照name进行排序;
*
*
*
*
*
*
**/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合来存取对象
TreeSet<Student> ts = new TreeSet<Student>();
//添加对象
ts.add(new Student("zhansan",45));
ts.add(new Student("bhansan",35));
ts.add(new Student("chansan",25));
ts.add(new Student("ehansan",95));
ts.add(new Student("ohansan",45));
//获取迭代器 iter
Iterator iter = ts.iterator();
while(iter.hasNext()){
//获取取出的对象
Student stu = (Student)iter.next();
//打印对象的name和age
System.out.println(stu.getName()+"---"+stu.getAge());
}
}
}
//Student 实现Comparable接口 并覆盖compareTo的方法
class Student implements Comparable{
//属性name
private String name;
//属性age
private int 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;
}
public int compareTo(Object obj){
Student stu = (Student)obj;
if(this.getAge()>stu.getAge()){
return 1;
}else if(this.age<stu.getAge()){
return -1;
}
else{
if((this.name).compareTo(stu.getName())>0){
return 1;
} else if((this.name).compareTo(stu.getName())<0){
return -1;
}
else{
return 0 ;
}
}
}
Student(String name ,int age){
this.name = name;
this.age= age;
}
}
第二种方法,自定义比较器,传入TreeSet。并取出元素。
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Blog31 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//集合ts来存取数据,传入自己定义好的构造器,MyComparator
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
//存入数据
ts.add(new Student("zhansan",45));
ts.add(new Student("bhansan",35));
ts.add(new Student("chansan",25));
ts.add(new Student("ehansan",95));
ts.add(new Student("ohansan",45));
//取出数据
Iterator iter = ts.iterator();
while(iter.hasNext()){
Student stu = (Student)iter.next();
System.out.println(stu.getName()+"---"+stu.getAge());
}
}
}
//自定义的比较器,实现Comparator的 compare方法
class MyComparator implements Comparator{
//覆盖compare
public int compare(Object obj1,Object obj2){
//传入两个对象
Student stu1 = (Student)obj1;
Student stu2 = (Student)obj2;
//按照年龄的大小进行排序
if(stu1.getAge()>stu2.getAge()){
return 1;
}else if(stu1.getAge()<stu2.getAge()){
return -1;
}
else{
//如果年龄相同,就按照name这个属性的自然顺序进行比较
//全部相同则视为同一个对象,不存入;
if((stu1.getName()).compareTo(stu2.getName())>0){
return 1;
} else if((stu1.getName()).compareTo(stu2.getName())<0){
return -1;
}
else{
return 0 ;
}
}
}
}