- Collection接口属于java.util包下,使用的时候需要导包。
- add(E e)方法:e代表任何对象,由于此函数永远返回true(set集合添加了相同的对象/重复的元素才会返回flase),所以此方法的返回值通常不赋值给任何变量(接受此返回值没有意义)。
- add(E e)方法在接收基本数据类型时会自动装箱。
Collection clt = new ArrayList();
boolean bool_1 = clt.add(true);// 相当于clt.add(new Boolean(true));
boolean bool_2 = clt.add(100);// 相当于clt.add(new Integer(100));
- Collection和ArrayList的继承体系中的中间某个类(ArrayList的父类的父类),重写了toString()方法,所以打印ArrayList引用的时候,输出的结果不是Object类中的默认的toString()结果。
- 使用Collection的时候如果不加泛型会有警告,解决办法是在类的上面添加注解:
@SuppressWarnings({ "rawTypes", "unchecked" })
- 其它常用方法:
boolean remove(Object o)
void clear()
boolean contains(Object o)
boolean isEmpty()
int size()