Collection接口

Collection接口表示一组对象,这些对象也称为Collection接口的元素。一些Collection接口允许有重复的元素,而另一些则不允许;一些Collection接口是有序的,而另一些是无序的。

Collection类有众多的API。下面是官方提供的API。

Modifier and TypeMethodDescription
booleanadd​(E e)
Ensures that this collection contains the specified element (optional operation).
booleanaddAll​(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation).
voidclear​()
Removes all of the elements from this collection (optional operation).
booleancontains​(Object o)
Returns  true if this collection contains the specified element.
booleancontainsAll​(Collection<?> c)
Returns  true if this collection contains all of the elements in the specified collection.
booleanequals​(Object o)
Compares the specified object with this collection for equality.
inthashCode​()
Returns the hash code value for this collection.
booleanisEmpty​()
Returns  true if this collection contains no elements.
Iterator<E>iterator​()
Returns an iterator over the elements in this collection.
default Stream<E>parallelStream​()
Returns a possibly parallel  Stream with this collection as its source.
booleanremove​(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).
booleanremoveAll​(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation).
default booleanremoveIf​(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate.
booleanretainAll​(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).
intsize​()
Returns the number of elements in this collection.
default Spliterator<E>spliterator​()
Creates a  Spliterator over the elements in this collection.
default Stream<E>stream​()
Returns a sequential  Stream with this collection as its source.
Object[]toArray​()
Returns an array containing all of the elements in this collection.

在Collection对象中并为提供get()方法获取元素。如果要遍历Collection中的元素,一般采用Iterator遍历器。Collection接口继承了接口Iterable<E>,实现这个接口将允许对象成为foreach语句的目标。在Iterable中提供了迭代方法iterator(),所以Collection接口定义的子类集合上都有一个与容器类相对应的遍历器。可以通过遍历器遍历出容器类中的各个元素对象。

Iterator接口中定义的方法如下。

Modifier and TypeMethodDescription
default voidforEachRemaining​(Consumer<? super E> action)
Performs the given action for each remaining element until all elements have been processed or the action throws an exception.
booleanhasNext​()
Returns  true if the iteration has more elements.
Enext​()
Returns the next element in the iteration.
default voidremove​()
Removes from the underlying collection the last element returned by this iterator (optional operation).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值