Java(HashCode应用和框架概念)

 

ArrayList与HashSet的比较以及HashCode分析

package com.itheima;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class ReflectTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	//	Collection collections = new ArrayList();
		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3,3);
		ReflectPoint pt2 = new ReflectPoint(5,5);
		ReflectPoint pt3= new ReflectPoint(3,3);
		
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);
		
		pt1.y = 7;//hashCode值已经改了
		collections.remove(pt1);//hashcode区域改了,没有删除
		System.out.println(collections.size());
		/*当一个对象被存储进HashSet集合中以后,就不能修改这个对象中的那些参与计算哈希值的字段了,
		否则,对象修改后的哈希值与最初存储进HashSet集合中的哈希值就不用了,
		在这种情况下,即使在contains方法使用该对象的当前引用作为参数去HashSet集合中检索对象,也将返回找不到对象的结果,
		这也会导致无法从HashSet集合中单独删除当前对象,从而造成内存泄露。
		*/
	}

}

package com.itheima;

public class ReflectPoint {

	private int x;
	public int y;
	public String str1 = "ball";
	public String str2 = "basketball";
	public String str3 = "itcast";
	
	
	public ReflectPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ReflectPoint other = (ReflectPoint) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "ReflectPoint [x=" + x + ", y=" + y + ", str1=" + str1
				+ ", str2=" + str2 + ", str3=" + str3 + "]";
	}
	
	
}


运行结果:

当没有覆写equals和hashCode时候

4

3

覆写之后

3

2

System.out.println(pt1.equals(pt3));

false

------------------------------------------------------------------------

反射的作用——>实现框架功能

框架与框架要解决的核心问题
我做房子卖给用户住,由用户自己安装门窗和空调,我做的房子就是框架,用户需要使用我的框架,把门窗插入进我的框架中。
框架与工具类有区别,工具类被用户的类调用,而框架则是调用用户提供的类。
框架要解决的核心问题
因为在才写程序时无法知道要被调用的类名,所以,在程序中无法直接new某个类的实例对象了,而要用反射方式来做。
不出现具体的类的名称,而是从配置文件里读取出来。
配置文件:
package com.itheima;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

public class ReflectTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
//		Collection collections = new ArrayList();
		
		InputStream ips = new FileInputStream("config.properties");//尽量面向父类接口编程,IPS是一个流
		Properties props = new Properties();//Properties对象相当于一个hashmap,还扩展了自己内存的键值对存到硬盘里,也能在初始化时,把键值对加载进来。
		props.load(ips);
		ips.close();//释放关联的系统资源
		String className = props.getProperty("className");
		Collection collections = (Collection)Class.forName(className).newInstance();
	
//		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3,3);
		ReflectPoint pt2 = new ReflectPoint(5,5);
		ReflectPoint pt3= new ReflectPoint(3,3);
		
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);
		
//		pt1.y = 7;//hashCode值已经改了
		collections.remove(pt1);//hashcode区域改了,没有删除
		System.out.println(collections.size());
		System.out.println(pt1.equals(pt3));
		/*当一个对象被存储进HashSet集合中以后,就不能修改这个对象中的那些参与计算哈希值的字段了,
		否则,对象修改后的哈希值与最初存储进HashSet集合中的哈希值就不用了,
		在这种情况下,即使在contains方法使用该对象的当前引用作为参数去HashSet集合中检索对象,也将返回找不到对象的结果,
		这也会导致无法从HashSet集合中单独删除当前对象,从而造成内存泄露。
		*/
	}

}

打印结果:

3

true

如果改为java.util.HashSet

结果为

1

true

------------------------------------------------------------------------------

用类加载器的方式管理资源和配置文件

package com.itheima;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

public class ReflectTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
//		Collection collections = new ArrayList();
		
		//一定要记住用完成的路径,但完整的路径不是硬编码,而是运算出来的、//尽量面向父类接口编程,IPS是一个流
	//	InputStream ips = new FileInputStream("config.properties");
		//类加载起加载的一种通用方式,通过.class找到类加载器,例子:喝可乐,你每次都在我,我就找商家,我干脆内部提供卖可乐的方法
	//	InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("com/itheima/config.properties");
		//内部提供的方法。相对路径
		InputStream ips = ReflectTest2.class.getResourceAsStream("config.properties");
		Properties props = new Properties();//Properties对象相当于一个hashmap,还扩展了自己内存的键值对存到硬盘里,也能在初始化时,把键值对加载进来。
		props.load(ips);
		ips.close();//释放关联的系统资源
		String className = props.getProperty("className");
		Collection collections = (Collection)Class.forName(className).newInstance();
	
//		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3,3);
		ReflectPoint pt2 = new ReflectPoint(5,5);
		ReflectPoint pt3= new ReflectPoint(3,3);
		
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);
		
//		pt1.y = 7;//hashCode值已经改了
		collections.remove(pt1);//hashcode区域改了,没有删除
		System.out.println(collections.size());
		System.out.println(pt1.equals(pt3));
		/*当一个对象被存储进HashSet集合中以后,就不能修改这个对象中的那些参与计算哈希值的字段了,
		否则,对象修改后的哈希值与最初存储进HashSet集合中的哈希值就不用了,
		在这种情况下,即使在contains方法使用该对象的当前引用作为参数去HashSet集合中检索对象,也将返回找不到对象的结果,
		这也会导致无法从HashSet集合中单独删除当前对象,从而造成内存泄露。
		*/
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值