public interface Collection<E> extends Iterable<E> {
//return集合中元素数量[如果大于Integer.MAX_VALUE则返回Integer.MAX_VALUE]
int size();
//return是否集合中不包含任何元素
boolean isEmpty();
//return当前集合是否包含指定元素o
//throws ClassCastException 指定元素o与集合不兼容
//throws NullPointerException 当前集合不允许null&&指定元素o为null
boolean contains(Object o);
//return当前集合的Iterator
Iterator<E> iterator();
//return包含当前集合所有元素的数组
Object[] toArray();
//return包含当前集合所有元素的数组[精确控制运行时类型]
//throws ArrayStoreException 如果T不是每个元素的运行时类型的超类型(父类型)
<T> T[] toArray(T[] a);
//return集合是否改变
//throws UnsupportedOperationException 当前集合不支持add操作
//throws ClassCastException 指定元素的类E不允许add
//throws NullPointerException 当前集合不允许null值add
//throws IllegalArgumentException 不允许元素的某些属性add
//throws IllegalStateException 在某些时机不允许add
boolean add(E e);
//return是否删除某个元素
//throws ClassCastException 当前集合与元素o不兼容
//throws NullPointerException 当前集合不允许null值(即o为nul)
//throws UnsupportedOperationException 当前集合不允许remove
boolean remove(Object o);
//return当前集合是否包含c集合中的所有元素
//throws ClassCastException 当前集合与c集合存在元素不兼容
//throws NullPointerException 当前集合不允许null值,c中包含null值
boolean containsAll(Collection<?> c);
//return集合是否改变
//throws UnsupportedOperationException 当前集合不支持addAll操作
//throws ClassCastException 指定元素的类E不允许addAll
//throws NullPointerException 当前集合不允许null值addAll
//throws IllegalArgumentException 不允许元素的某些属性addAll
//throws IllegalStateException 在某些时机不允许addAll
boolean addAll(Collection<? extends E> c);
//return集合是否改变
//throws UnsupportedOperationException 当前集合不支持removeAll操作
//throws ClassCastException 存在元素不兼容
//throws NullPointerException 当前集合不允许null值
boolean removeAll(Collection<?> c);
//return如果删除某个元素是否会抛出异常[默认遍历迭代器]
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
//return集合是否因此改变
//只会保留存在集合c中的元素
//throws UnsupportedOperationException 当前集合不支持retainAll
//throws ClassCastException 存在元素不兼容
//throws NullPointerException 当前集合不允许null值
boolean retainAll(Collection<?> c);
//删除当前集合所有元素
//throws UnsupportedOperationException 当前集合不允许clear
void clear();
boolean equals(Object o);
int hashCode();
//1.8新增
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
//1.8新增
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
//1.8新增
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}
}
Collection接口
最新推荐文章于 2023-04-17 14:48:26 发布