java 集合的使用

总结一下这两天复习的东东-《训练营2010年java高新技术》 ,对我最大的感受就是张老师讲的知识点有一定的深度,比如spring的aop,beanfactory,以前只知道使用,不知道其中的原理(spring应用了大量的反射技术),好了,下面大概做个知识点的总结

hashCode的作用:判断两个对象的哈希值是否相等,HashMap 和 ArrayList 的添加原则,HashMap 和 ArrayList 的添加原则,注解annotation,menu枚举的使用,泛型,反射,spring框架的aop,动态代理原理看例子吧,

 

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class HashCodeTest {
 public static void main(String[] args) throws Exception{
  // Collection c = new ArrayList();
  InputStream in = new FileInputStream("config.properties");
  //加载配置文件 用到  Properties 对象哈
  Properties props = new Properties();
  props.load(in);
  in.close();
  // java.util.HashSet
  String className = (String)props.get("className");
  Collection c = (Collection)Class.forName(className).newInstance();
  //Collection c = new HashSet();
  ReflectPoint p1 = new ReflectPoint(3, 3);
  ReflectPoint p2 = new ReflectPoint(4, 4);
  ReflectPoint p4 = new ReflectPoint(3, 3);
  /*
   * hashcode
   * Set 不容许相同的元素加入 每一个对象要存入set集合, 都要计算hashcode值,根据内存地址来
   * 计算的(这个是你没有复写hashCode()方法),你知道吗, 所以这个肯定不相等,
   */
  c.add(p1);
  c.add(p2);
  c.add(p4);
  // 没有复写ReflectPoint equals 方法时
  // 当HashSet时添加元素时,必须检查有没有这个元素
  System.out.println(c.size());
   p1.x = 8;
   //对象被加入到Set集合中去,就不要去修改那些影响
   //hashcode值的元素了,不然就无法删除rm,照成内存溢出
   c.remove(p1);
  // 内存地址
  System.out.println(p1 == p4);
  // 复写equals方法,一般是比较它们的内容,所以相等
  System.out.println(p1.equals(p4));

  System.out.println(c.size());
 }
}

 


public class ReflectPoint {

 public int x;
 private int y;
 
 public ReflectPoint(int x, int y) {
  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;
 }
}
 

config.properties属性文件的内容

className=java.util.HashSet

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值