Collection是Java集合框架顶层接口,不提供实现,不能直接实例化;其操作依赖equals()而非==;遍历时需用Iterator安全删除;线程安全、顺序、去重等由具体实现类决定。
Collection 接口是 Java 集合框架的顶层契约,它不提供具体实现,但强制所有单列集合(如 ArrayList、HashSet、LinkedList)必须支持一套基础操作——换句话说,它是“能叫集合”的最低标准。
因为 Collection 是接口,JDK **明确不提供它的直接实现类**。你写 Collection 会编译报错:Error: Cannot instantiate the type Collection。
Collection
Collection c = new ArrayList(); 或 new LinkedHashSet()
public void processItems(Collectionitems) { for (String s : items) { System.out.println(s); } } // 调用时可传入 ArrayList、HashSet、LinkedList... 不用改方法签名 processItems(new ArrayList<>()); processItems(new HashSet<>());
所有基于 Collection 的操作(比如 contains()、remove()、containsAll())都依赖元素自身的 equals() 方法,而不是 ==。
Person),没重写 equals() 和 hashCode(),那 contains(new Person("Alice")) 几乎一定返回 false,哪怕集合里真有同名对象remove() 只删第一个匹配项;removeAll() 删的是差集,不是“全部删光”retainAll() 是取交集,且会**修改原集合**——这点常被误认为是“只读操作”二者底层都调用 iterator(),但行为边界不同:
for-each 是语法糖,简洁安全,但**无法在遍历时删除元素**(会抛 ConcurrentModificationException)Iterator 并调用 it.remove()
Iterator 是唯一被 Collection 强制要求支持的遍历方式(通过继承 Iterabl
e),所以它是通用性最高的遍历入口Collectionnames = new ArrayList<>(Arrays.asList("a", "b", "c")); Iterator it = names.iterator(); while (it.hasNext()) { String s = it.next(); if ("b".equals(s)) it.remove(); // ✅ 安全删除 } // names 现在是 ["a", "c"]
真正容易被忽略的点是:Collection 接口本身不保证线程安全、不保证顺序、不保证去重——这些特性全由子接口(List / Set / Queue)及其具体实现类决定。选错实现类,再熟的 Collection 方法也救不了逻辑 bug。