JAVA学习.JAVA集合类型Collection.Set.HashSet&TreeSet

以下是两张比较简单的介绍图,在具体代码分析之前先大概的了解以下要介绍的内容。



要插入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:blue Weight:111
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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值