java set retainall_Java的Set集合中的retainAll()方法

我们来看官方文档:

boolean retainAll​(Collection> c)

Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.

翻译过来,简要意思就是:仅仅保留这个 set 中同时也在指定的 collection 中(即参数C)的元素。换句话来说,就是移除这个 set 中的所有的不包含在指定的 collection 中的元素。如果指定的 collection 也是一个 set,这个操作实际上就会修改调用这个方法的 set,使之剩下的元素为这两个 set 的交集。

Specified by:

retainAll in interface Collection

Parameters:

c - collection containing elements to be retained in this set

Returns:

true if this set changed as a result of the call

Throws:

UnsupportedOperationException - if the retainAll operation is not supported by this set

ClassCastException - if the class of an element of this set is incompatible with the specified collection (optional)

NullPointerException - if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值