站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题TreeSet中的元素的排列问题?

TreeSet中的元素的排列问题?

摘要: TreeSet中的元素的排列问题?


我是初学者。
TreeSet中的元素的排列是怎样的,或者是不是需要自己排。
比如我在TreeSet中存了N个对象,一个对象又有N个属性,如果我想取得一个对象的特定属性怎么办?
也许说的不清楚,请高手指点。


根据存入对象定义的Comparator排序


class a implements Comparator{
int compare (Object o,Object o2){
这里强制转型你存储的对象,然后比较
返回1,0,-1
}
boolean equals(Object obj) {
}

}

new TreeSet(new a());


Student.java

package net.mengfanpp;

import java.util.Calendar;

/**
* 学生类
*
* @author mengfanpp
*
*/
public class Student {

private String id;

private String name;

private Boolean sex;

private Calendar birthday;



/**
* 学生类简单构造函数
*/
public Student() {
super();
// TODO Auto-generated constructor stub
}

/**
* @return Returns the birthday.
*/
public Calendar getBirthday() {
return birthday;
}

/**
* @param birthday
* The birthday to set.
*/
public void setBirthday(Calendar birthday) {
this.birthday = birthday;
}

/**
* @return Returns the id.
*/
public String getId() {
return id;
}

/**
* @param id
* The id to set.
*/
public void setId(String id) {
this.id = id;
}

/**
* @return Returns the name.
*/
public String getName() {
return name;
}

/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}

/**
* @return Returns the sex.
*/
public String getSex() {
return sex == true ? "男":"女";
}

/**
* @param sex
* The sex to set.
*/
public void setSex(Boolean sex) {
this.sex = sex;
}

//在定义类时,最好总是重新定义equals()与hashCode()方法以及toString(),以符合Java的设计规范。

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object other) {
// TODO Auto-generated method stub
if(this == other)
return true;

if(!(other instanceof Student))
return false;

final Student stud = (Student) other;

if(!id.equals(stud.getId())){
return false;
}

return true;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
// TODO Auto-generated method stub
return id.hashCode();
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder studInfo = new StringBuilder("");
studInfo.append("学号:"+id+" 的学生姓名为:"+name+",性别为:"+this.getSex()+",出生日期为:"+DateInfo.getStrFromDateTime(birthday,"yyyy-MM-dd") );

return studInfo.toString();
}

}


StudentComparator.java

package net.mengfanpp;

import java.util.Comparator;

/**
* @author mengfanpp
*
*/
public class StudentComparator implements Comparator {

/* (non-Javadoc)
* @see java.util.Comparator#compare(T, T)
*/
public int compare(Student arg0, Student arg1) {
// TODO Auto-generated method stub

//按学号正序排列
//return arg0.getId().compareTo(arg1.getId());

//按出生日期正序排列
//return arg0.getBirthday().compareTo(arg1.getBirthday())

//按出生日期倒序排列
return arg0.getBirthday().compareTo(arg1.getBirthday()) * -1;
}
}


/**
*
*/
package net.mengfanpp;

import java.util.TreeSet;
import java.util.Set;
import java.util.Comparator;

/**
* 练习使用TreeSet,主要练习了排序(StudentComparator定义了排序规则)
*
* @author mengfanpp
*
*/
public class TreeSetDemo3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Comparator comparator = new StudentComparator();
Set set = new TreeSet(comparator);

Student stud1 = new Student();
Student stud2 = new Student();
Student stud3 = new Student();
//Student stud4 = new Student();

stud1.setId("002");
stud1.setName("张三");
stud1.setSex(true);
stud1.setBirthday(DateInfo.getDateTimeFromStr("1979-03-12","yyyy-MM-dd"));

stud2.setId("001");
stud2.setName("李斯");
stud2.setSex(true);
stud2.setBirthday(DateInfo.getDateTimeFromStr("1980-05-16","yyyy-MM-dd"));

stud3.setId("003");
stud3.setName("良葛格");
stud3.setSex(true);
stud3.setBirthday(DateInfo.getDateTimeFromStr("1975-08-10","yyyy-MM-dd"));


set.add(stud1);
set.add(stud2);
set.add(stud3);

// 使用增强的for循环显示对象
for (Student stud : set) {
System.out.println(stud.toString() + " ");
}
System.out.println();

}

}



public class TreeSet
extends AbstractSet
implements SortedSet, Cloneable, Serializable
由上面可以看出,其中的对象是有序的,而你又没有提供排序功能,那根据什么排序的呢?
文档描述如下
public interface SortedSet
extends Set
A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.)
如果你需要自己定义排序的行为,如楼上所说


梦凡老是贴老长的代码~~
让人看不到重点


http://blog.csdn.net/XXKKFF


↑返回目录
前一篇: MapTreeSet,急!!!小弟跪求答案~~~。
后一篇: 老知识,新问题-----关于汉字文本编码