Java Container Learning notes

1.  Above all, keep below diagram in mind:

2.  Set 特点: 无序,无重复元素。 元素重复的标准是它们是否equals()。

3.  contains(Object), remove(Object)方法的判断标准依然是equals与否

     container.add(new Integer(100));

     container.remove(new Integer(100)); //此时原先added的100会被remove

     而对于自定义类, 需要重写equals()和hashCode() (当作为map中的key时用到,效率更高)方法以实现自定义类型的相等规则:

      

 

4.  容器不能容纳基础类型数据的原因是这种数据往往分配在栈上,而栈上的数据很容易被清空。

5.  If two object are equal according to the equals() method, then calling the method hashCode() on each of the 2   

     objects must produce the same integer results.

     However, it is no required that if two objects are unequal, then calling the hashCode() on each of the 2 objects

     must produce distinct results. But, the programmer should be aware that producing distinct integer results for

     unequal objects may improve the performance of hashtables.

6.  Iterator的remove()方法是在迭代过程中删除元素的唯一安全方法。即在使用iterator时,不能用collection的remove(OBJ)方法,

     此时实际上collection被lock了。

7.  jdk1.5enhance了for语句:

    

     缺陷:

     I. 对数组而言,不能方便地访问下标值

     II.对集合而言,与iterator 相比,不能方便地删除集合中的内容,但其实其遍历也是通过iterator实现。

     故除了简单遍历读内容外,Enhanced for is not that good.

8.  To make 2 objects of a class comparable, we need to let the class implement the interface comparable .

     And rather, override the compareTo() method

9.  collection的方法传参及返回值基本上都是Object类型,故使用时要注意类型转换。这也是引入泛型的原因之一。

10. JDK1.5 new feature : auto-boxing/unboxing      int <-> Integer.

      map.put("one", 1) ; //legal    automatically changed into map.put("one", new Integer(1));

      int i = (Integer)map.get("one");

11. 在使用collection时,尽量使用泛型。安全且可读性强

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值