HashCode的作用:
存放:
使用了hash算法的集合,存放的数据是无序的,唯一的,在存放的时候,会先将这个值使用hash算法(将集合分成若干个区域),得到一个hashCode值,先在相应区域判断这个hashCode值是否存在,如果不存到则将值存放到相应的区域,如果存在则不保存。所以在一个实体当中要重写equals方法,一般也会重写hashCode方法
如果在存完一个值后,又对这个对象重装赋值,则原hashCode码不会改变同,如果这时删除这个对象的时候,则无法删除,因为删除的时候也是按照hashCode删除的,由此会导致内在泄漏。
内省JAVABean的使用
优点总结:
过通内省的方式可以比反射的方式更快更精准的得到想到的参数的值,通常用于配合反射使用,在反射取得类的对象后,如果要操作的类是JAVABEAN则通过JAVAEAN的方法取得参数,但类必须是JAVABEAN的才可以用这种方法
使用示例:
Public Class Student{
Private String id;
Public String name;
Public void setId(String id){
This.id = id;
}
Public void setName(String name){
This.name= name;
}
Public String getId(){
Return this.id;
}
Public String getName(){
Return this.Name;
}
}
执行示例:
通过内省得到name的值
PropertyDescriptor pd = new PropertyDescriptor(“属性名”,类名.class);
Method method =pd.getReadMethod();
String retValue=Method.invoke();
Method method =pd.setWriterMethod();
Method.invoke(“zhangsan”);
try {
Userinfo u=Userinfo.class.getConstructor(Userinfo.class).newInstance();
PropertyDescriptor pd = new PropertyDescriptor("username",
Userinfo.class); // 这里的字节码必须是JAVABEAN类的字节码
Method method1 = pd.getReadMethod(); // 取得get方法
Method method2 = pd.getWriteMethod();// 取得set方法
method1.invoke(对象,参数); 在这里必须指定是哪个对象的方法,比如不同的对象传进去的参数值也不同,所以必须有已经生成的对象
} catch (IntrospectionException e) {
e.printStackTrace();
}
上面代码用Work编辑的。有些字母大小写不对
BeanUtils类
使用本类的好处:对JAVABean类操作的更加方便,可以获取设置值,将信息转换为Map和将map的信息转换为JAVABean
使用场景:一般用于配合**反射**使用 将对象取出的时候,对对象进行赋值和取值的操作 实体与map相互转换的时候使用
使用示例:
Userinfo u = new Userinfo();
BeanUtils.setProperty(u, "id", "yy"); //将值通过beanUtil工具写入实体对象中
System.out.println(BeanUtils.getProperty(u, "id")); //使用beaUtils读取出实体对象中的值
Date d = new Date();
BeanUtils.setProperty(d, "d.year", 1015); //如果该对象还有一下级属性,也可以对于确定的下一级属性直接赋值
Map map = new HashMap();
map.put("id", "aa");
BeanUtils.populate(u, map); //将一个map转换为一个实体对象
BeanUtils.describe(u); // 将一个实体对象转换为map
PropertyUtils
在jdk1.5之前一直用这个。。。。上BeanUtils使用方法差不多,不过赋值的时候,原类型是什么类型就赋值是什么类型,
而BeanButils会将值转换为String再进行赋值操作