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时,尽量使用泛型。安全且可读性强