以下是两张比较简单的介绍图,在具体代码分析之前先大概的了解以下要介绍的内容。
要插入Set中的自定义对象,一般的基本类型已经实现了hashcode、equals以及Comparable接口。
/**
*
*/
package CollectionDemo.SetDemo;
/**
* @author fshxxxyydys
*
*/
public class Apple implements Comparable<Apple> {
//如果要使得对象可以在TreeSet中
//必须实现Comparable接口并实现compareTo方法
private String color;
private Integer weight;
public Apple(String color, Integer weight) {
super();
this.color = color;
this.weight = weight;
}
public Apple() {
super();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
@Override
public boolean equals(Object obj) {
System.out.println("Apple equals method is invoked!");
//由于传进来的Object对象,所以要进行判断
if(obj == null) return false;
if(obj == this) return true;
if(!(obj instanceof Apple)) return false;
Apple otherapple = (Apple)obj;
if(this.color.equals(otherapple.color) && this.weight.equals(otherapple.weight))
return true;
return false;
}
@Override
public int hashCode() {
//object.hashcode对于任何对象来说,运行结果都是不一样的
//即使该对象中的属性全部都相等,所以我们可以理解为hashcode
//返回的是该对象在内存中的地址
System.out.println("Apple Hashcode method is invoked!");
return this.color.hashCode() + this.weight.hashCode();
//重写的时候可以按照每种
//基本类型hashcode值的和来进行初步判断
}
@Override
public int compareTo(Apple o) {
//System.out.println("Apple compareTo method is invoked!");
if(o.color.compareTo(this.color)!= 0){
//如果颜色不同就先按照颜色排序
return this.color.compareTo(o.color);
}
else if(o.weight.compareTo(this.weight) != 0){
//在颜色相同的情况下,如果重量不相等就再按照重量来排序
return this.weight.compareTo(o.weight);
}
//可以根据定义的对象来具体决定要进行怎么的排列顺序
//以及按照什么来进行排序,决定先按哪个排再按哪个排
return 0;
//注:如果上面都不满足则返回0。
//表示两个对象相同,则不进行插入。
//该方法实际上是边排序边进行筛选。
}
@Override
public String toString() {
return "Apple:"+this.color +" Weight:" +this.weight ;
}
}
HashSet:
/**
*
*/
package CollectionDemo.SetDemo;
import java.util.HashSet;
/**
* @author fshxxxyydys
*
*/
public class HashSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
//必须重载hashcode和equals方法才能完成剔除重复的元素的任务
HashSet<Apple> appleSet = new HashSet<Apple>();
appleSet.add(new Apple("red",120));
appleSet.add(new Apple("red",120));
System.out.println(appleSet.size());
System.out.println((new Apple("bule",120)).hashCode());
System.out.println((new Apple("bule",120)).hashCode());
//hashSet先调用hashcode()初步判断两个对象是否相等
//根据hashcode()生成的内存地址
//一般将其改写为每一个属性的hashcode值相加,来进行初步判断
//如果初步判断相等则调用高级判断也就是equals()来进行最终的判断
//决定两个对象是否相等,若相等则不将新对象加入hashset容器中,如果不相等
//否决hashcode的初步判断,将新对象加入hashset容器中。
//总结:如果一个对象需要保存在HashSet中就必须重写hashcode和equals方法。
}
}
====================================================================
Result:
Apple Hashcode method is invoked!
Apple Hashcode method is invoked!
Apple equals method is invoked!
1
Apple Hashcode method is invoked!
3035524
Apple Hashcode method is invoked!
3035524
====================================================================
TreeSet:
/**
*
*/
package CollectionDemo.SetDemo;
import java.util.TreeSet;
/**
* @author fshxxxyydys
*
*/
public class TreeSetDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//拥有天然顺序 ,其排序的规则是对象类自身携带的
//TreeSet中的元素是不能雷同的,雷同性判断并不依
//hashcode和equals方法,而是依靠compareTo方法。
TreeSet<Apple> appleSet = new TreeSet<Apple>();
appleSet.add(new Apple("bule",120));
appleSet.add(new Apple("yellow",120));
appleSet.add(new Apple("red",119));
appleSet.add(new Apple("bule",115));
appleSet.add(new Apple("red",125));
appleSet.add(new Apple("blue",111));
appleSet.add(new Apple("red",111));
appleSet.add(new Apple("yellow",112));
appleSet.add(new Apple("yellow",118));
System.out.println("元素个数:" + appleSet.size());
for(Apple a:appleSet)
System.out.println(a);
}
}
====================================================================
Result:
元素个数:9
Apple:bule Weight:115
Apple:bule Weight:120
Apple:red Weight:111
Apple:red Weight:119
Apple:red Weight:125
Apple:yellow Weight:112
Apple:yellow Weight:118
Apple:yellow Weight:120
====================================================================
原创出处:http://blog.csdn.net/u012830807