java set collection_Java的Collection接口和Set接口?

它们外部行为一样,但潜在行为不一样。

Collection 的说明中明确说明了 Collection 是集合类似的根接口,包含一组对象,他们可能是可以重复的,也有可能不是。有些可能是排序的,有些可能不是……也就是说,它表示集合,但潜在行为并不确定。The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

而 Set 的潜在行为就确定得多,它明确了所包含的内容是不可重复的。但并没有说明排序的问题。不过它有一个SortedSet 子接口申明了内容有序。A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

所以虽然接口成员一样,但是潜在行为并不一样。当你只需要一组数据而不关系它是否重复,是否有序的时候,可以直接用 Collection 接口,但是如果你需要的是一组不可重复的数据,那显然应该用 Set 接口。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值