Java(HashCode应用和框架概念)

来源:互联网 发布:ping 域名ip 编辑:程序博客网 时间:2024/06/03 16:52


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;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic 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;}@Overridepublic 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集合中单独删除当前对象,从而造成内存泄露。*/}}


原创粉丝点击