JAVA TreeSet体会_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA TreeSet体会

JAVA TreeSet体会

 2012/6/11 0:11:50  zzq.at.SOARingLab  程序员俱乐部  我要评论(0)
  • 摘要:一直以来,对TreeSet的理解受到了HashMap的影响。认为HashSet也是通过equals方法区分对象。最近在一个程序开发中使用了TreeSet,发现TreeSet区分对象是根据对象实现的Comparable或者Comparator接口中的Compare()方法或者CompareTo()方法。如果两个对象通过比较返回值为0,那么将一个对象插入到TreeSet后,另外一个对象将不能插入TreeSet。HashSet通过HashMap实现,集合中的每一个对象都是一个key
  • 标签:Java
    一直以来,对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();
	}
}

发表评论
用户名: 匿名