import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;public class NameSort implements Comparable{
private String name;
private int age;
private String no;
public NameSort(String name,int age, String no){
this.name = name;
this.age = age;
this.no = no;
}
public int compareTo(Object ob) {
NameSort o = (NameSort)ob;
if(this.age > o.age){
return 1;
}
if(this.age <o.age){
return -1;
}
if(this.name.compareTo(o.name) > 0 ){
return 1;
}
if(this.name.compareTo(o.name) < 0 ){
return -1;
}
/* if(this.no.compareTo(o.no) > 0){
return 1;
}
if(this.no.compareTo(o.no) < 0){
return -1;
}
*/
return 0;
}
public boolean equals(Object ob){
System.out.println("OK");
if(this == ob)return true;
if(!(ob instanceof NameSort))return false;
final NameSort other = (NameSort)ob;
if(this.name.equals(other.name)&&this.age==other.age&&this.no.equals(other.no)){
return true;
}else{
return false;
}
}
public int hashCode(){
System.out.println("OK");
int result ;
result = (this.name == null?0:this.name.hashCode());
result = 29*result+age;
return result;
}
public static void main(String[] args) {
NameSort n6 = new NameSort("meimei",18, "1");
NameSort n1 = new NameSort("zhaowei",23, "001");
NameSort n2 = new NameSort("xiaopang",22, "003");
NameSort n3 = new NameSort("meimei",21, "002");
NameSort n4 = new NameSort("meimei",18, "1");
NameSort n5 = new NameSort("meimei",18, "3");
Set<NameSort> set = new TreeSet<NameSort>();
set.add(n2); set.add(n1);set.add(n3);
set.add(n4);set.add(n5);set.add(n6);
Iterator<NameSort> it = set.iterator();
while(it.hasNext()){
NameSort n = it.next();
System.out.println(n.name+" "+n.age);
}
}
}
这段代码输出的结果是:meimei 18 meimei 21 xiaopang 22 zhaowei 23
并没有输出OK,证明当我们在向集合set加入元素的时候仅仅只调用了方法compareTo().但是很多书上却让我们在向集合里加入
对象时重写equals和hashCode方法.当然像HashSet这样的集合我们需要重写这俩个方法.但是象TreeSet这样的我们也需要重写吗?我觉得没有必要了.请大家指正我现在思路上的错误,谢谢哈.
import java.util.Set;
import java.util.TreeSet;public class NameSort implements Comparable{
private String name;
private int age;
private String no;
public NameSort(String name,int age, String no){
this.name = name;
this.age = age;
this.no = no;
}
public int compareTo(Object ob) {
NameSort o = (NameSort)ob;
if(this.age > o.age){
return 1;
}
if(this.age <o.age){
return -1;
}
if(this.name.compareTo(o.name) > 0 ){
return 1;
}
if(this.name.compareTo(o.name) < 0 ){
return -1;
}
/* if(this.no.compareTo(o.no) > 0){
return 1;
}
if(this.no.compareTo(o.no) < 0){
return -1;
}
*/
return 0;
}
public boolean equals(Object ob){
System.out.println("OK");
if(this == ob)return true;
if(!(ob instanceof NameSort))return false;
final NameSort other = (NameSort)ob;
if(this.name.equals(other.name)&&this.age==other.age&&this.no.equals(other.no)){
return true;
}else{
return false;
}
}
public int hashCode(){
System.out.println("OK");
int result ;
result = (this.name == null?0:this.name.hashCode());
result = 29*result+age;
return result;
}
public static void main(String[] args) {
NameSort n6 = new NameSort("meimei",18, "1");
NameSort n1 = new NameSort("zhaowei",23, "001");
NameSort n2 = new NameSort("xiaopang",22, "003");
NameSort n3 = new NameSort("meimei",21, "002");
NameSort n4 = new NameSort("meimei",18, "1");
NameSort n5 = new NameSort("meimei",18, "3");
Set<NameSort> set = new TreeSet<NameSort>();
set.add(n2); set.add(n1);set.add(n3);
set.add(n4);set.add(n5);set.add(n6);
Iterator<NameSort> it = set.iterator();
while(it.hasNext()){
NameSort n = it.next();
System.out.println(n.name+" "+n.age);
}
}
}
这段代码输出的结果是:meimei 18 meimei 21 xiaopang 22 zhaowei 23
并没有输出OK,证明当我们在向集合set加入元素的时候仅仅只调用了方法compareTo().但是很多书上却让我们在向集合里加入
对象时重写equals和hashCode方法.当然像HashSet这样的集合我们需要重写这俩个方法.但是象TreeSet这样的我们也需要重写吗?我觉得没有必要了.请大家指正我现在思路上的错误,谢谢哈.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货