JAVA TreeSet体会

一直以来,对TreeSet的理解受到了HashMap的影响。认为HashSet也是通过equals方法区分对象。最近在一个程序开发中使用了TreeSet,发现TreeSet区分对象是根据对象实现的Comparable或者Comparator接口中的Compare()方法或者CompareTo()方法。如果两个对象通过比较返回值为0,那么将一个对象插入到TreeSet后,另外一个对象将不能插入TreeSet。 HashSet通过HashMap实现,集合中的每一个对象都是一个key,根据根据对象的HashCode和equals方法区分。
可以通过修改以下代码查看结果。

import java.util.*;
public class TestTreeSet {

/**
* @param args
*/
public static void main(String[] args) {
TreeSet<TestClass> treeSetWithComparator = new TreeSet<TestClass>(new TestClassComparator());
HashSet<TestClass> testHashSet = new HashSet<TestClass>();
TestClass testObj1 = new TestClass(5);
TestClass testObj2 = new TestClass(5);
System.out.printf("\ntestObj1.getI == %d && testObj2.getI == %d\n",testObj1.getI(),testObj2.getI());
System.out.println("testObj1 equals testObj2? " + testObj1.equals(testObj2));
System.out.println("testObj1 compares with testObj2? " + treeSetWithComparator.comparator().compare(testObj1, testObj2));
System.out.println("testObj1 == testObj2? " + (testObj1 == testObj2));
System.out.println("Hashcode of testObj1 " + testObj1.hashCode());
System.out.println("Hashcode of testObj2 " + testObj2.hashCode());

System.out.println("Add testObj1 into treeset: " + treeSetWithComparator.add(testObj1));
System.out.println("Add testObj2 into treeset: " + treeSetWithComparator.add(testObj2));

System.out.println("Add testObj1 into HashSet: " + testHashSet.add(testObj1));
System.out.println("Add testObj2 into HashSet: " + testHashSet.add(testObj2));

treeSetWithComparator.clear();
testHashSet.clear();
testObj1.setI(5);
testObj2.setI(6);

System.out.printf("\ntestObj1.getI == %d && testObj2.getI == %d\n",testObj1.getI(),testObj2.getI());
System.out.println("testObj1 equals testObj2? " + testObj1.equals(testObj2));
System.out.println("testObj1 compares with testObj2? " + treeSetWithComparator.comparator().compare(testObj1, testObj2));
System.out.println("testObj1 == testObj2? " + (testObj1 == testObj2));
System.out.println("Hashcode of testObj1 " + testObj1.hashCode());
System.out.println("Hashcode of testObj2 " + testObj2.hashCode());

System.out.println("Add testObj1 into treeset: " + treeSetWithComparator.add(testObj1));
System.out.println("Add testObj2 into treeset: " + treeSetWithComparator.add(testObj2));

System.out.println("Add testObj1 into HashSet: " + testHashSet.add(testObj1));
System.out.println("Add testObj2 into HashSet: " + testHashSet.add(testObj2));

}

}

class TestClass{
private int i = 0;
public TestClass(int i){
this.i = i;
}
int getI(){
return i;
}
void setI(int i){
this.i = i;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestClass other = (TestClass) obj;
if (i != other.i)
return false;
return true;
}

}

class TestClassComparator implements Comparator<TestClass>{
@Override
public int compare(TestClass obj1, TestClass obj2) {
return obj1.getI() - obj2.getI();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值