1.TreeSet 是 Set 接口的实现类,TreeSet 可以确保集合元素处于排序状态;
2.TreeSet有两种排序方法——自然排序和自定义排序,默认情况下采用自然排序;
3.自然排序:
1️⃣TreeSet 会调用集合元素的 compareTo(Object obj) 方法来比较元素之间的大小关系,然后将集合元素按升序排列
2️⃣如果试图把一个对象添加到 TreeSet 时,则该对象的类必须实现 Comparable 接口。
3️⃣实现 Comparable 的类内必须实现 compareTo(Object obj) 方法,两个对象即通过 compareTo(Object obj) 方法的返回值来比较大小。
4️⃣向TreeSet内添加的对象应是同属一个类的;
5️⃣
public class Setree {
@Test
public void MM() {
Set setree = new TreeSet();
setree.add(new Beauty(12,"PG One"));
setree.add(new Beauty(23,"老流氓美美美"));
setree.add(new Beauty(24,"我科银河系第一美"));
setree.add(new Beauty(45,"皮二爷还是很美"));
setree.add(new Beauty(278,"科比为什么这么美"));
setree.add(new Beauty(2345,"Kobe一报由"));
Iterator iterator=setree.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
class Beauty implements Comparable{
private Integer BeautyNum;
private String name;
public Beauty(Integer BeautyNum, String name) {
this.BeautyNum = BeautyNum;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBeautyNum() {
return BeautyNum;
}
public void setBeautyNum(Integer beautyNum) {
BeautyNum = beautyNum;
}
@Override
public String toString() {
return "Name[name = " + this.getName() + " BeautyNumber[Num = " + this.getBeautyNum()+"]";
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Beauty mm = (Beauty)obj;
if (name == null) {
if (mm.name != null)
return false;
} else if (!name.equals(mm.name))
return false;
if (BeautyNum != mm.BeautyNum)
return false;
return true;
}
@Override
public int hashCode() {
final int kobe = 31;
int iverson = 3;
iverson = kobe * iverson + BeautyNum;
iverson = kobe * iverson + ((name == null) ? 0 : name.hashCode());
return iverson;
}
@Override
public int compareTo(Object o) {
if(o instanceof Beauty){
Beauty mm=(Beauty)o;
int num=this.getBeautyNum().compareTo(mm.getBeautyNum());
if(num==0){
return this.getName().compareTo(mm.getName());
}
else
return num;
}
return 0;
}
}
Name[name = PG One] BeautyNumber[Num = 12]
Name[name = 老流氓美美美] BeautyNumber[Num = 23]
Name[name = 我科银河系第一美] BeautyNumber[Num = 24]
Name[name = 皮二爷还是很美] BeautyNumber[Num = 45]
Name[name = 科比为什么这么美] BeautyNumber[Num = 278]
Name[name = Kobe一报由] BeautyNumber[Num = 2345]
4.自定义排序
1️⃣自定义排序即为让对象根据自己的特殊需求进行定制的排序操作;
2️⃣通过Comparator接口,重写其中的compare方法进行对象的比较,再将Comparator实例化的对象传入TreeSet中即可;
3️⃣
public class Treset {
@Test
public void MM(){
Comparator cp=new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Beauty&&o2 instanceof Beauty){
Beauty b1=(Beauty)o1;
Beauty b2=(Beauty)o2;
int i=b1.getBeautyNum().compareTo(b2.getBeautyNum());
if(i==0){
return b1.getName().compareTo(b2.getName());
}
return i;
}
return 0;
}
};
Set st=new TreeSet(cp);
st.add(new Beauty(12,"PG One"));
st.add(new Beauty(23,"老流氓美美美"));
st.add(new Beauty(24,"我科银河系第一美"));
st.add(new Beauty(45,"皮二爷还是很美"));
st.add(new Beauty(278,"科比为什么这么美"));
st.add(new Beauty(2345,"Kobe一报由"));
st.add(new Beauty(1,"妈妈最美"));
Iterator iterator=st.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
class Beauty implements Comparable{
private Integer BeautyNum;
private String name;
public Beauty(Integer BeautyNum, String name) {
this.BeautyNum = BeautyNum;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBeautyNum() {
return BeautyNum;
}
public void setBeautyNum(Integer beautyNum) {
BeautyNum = beautyNum;
}
@Override
public String toString() {
return "Name[name = " + this.getName() + "] BeautyNumber[Num = " + this.getBeautyNum()+"]";
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Beauty mm = (Beauty)obj;
if (name == null) {
if (mm.name != null)
return false;
} else if (!name.equals(mm.name))
return false;
if (BeautyNum != mm.BeautyNum)
return false;
return true;
}
@Override
public int hashCode() {
final int kobe = 31;
int iverson = 3;
iverson = kobe * iverson + BeautyNum;
iverson = kobe * iverson + ((name == null) ? 0 : name.hashCode());
return iverson;
}
@Override
public int compareTo(Object o) {
if(o instanceof Beauty){
Beauty mm=(Beauty)o;
int num=this.getBeautyNum().compareTo(mm.getBeautyNum());
if(num==0){
return this.getName().compareTo(mm.getName());
}
else
return num;
}
return 0;
}
}
Name[name = 妈妈最美] BeautyNumber[Num = 1]
Name[name = PG One] BeautyNumber[Num = 12]
Name[name = 老流氓美美美] BeautyNumber[Num = 23]
Name[name = 我科银河系第一美] BeautyNumber[Num = 24]
Name[name = 皮二爷还是很美] BeautyNumber[Num = 45]
Name[name = 科比为什么这么美] BeautyNumber[Num = 278]
Name[name = Kobe一报由] BeautyNumber[Num = 2345]