9.4.6 可选操作

9.4.6 可选操作

视窗(view)通常都存在一些限制——比如只能读取不能写入,比如不能改变size,比如支持删除但不支持插入(如Map的key view)。如果你试图对一个view进行不合适的操作,将会抛出UnSupportedOperationException。
在collection和iterator的API文档中,很多方法被描述为“optional operations”(可选操作)。这似乎违背了接口的本意。毕竟,接口的意义不正是规定了一个类必须要实现哪些方法吗?确实,API中的这种组织形式从理论上并不合理。一个更好的方案是:把这些有限制的view一个个分出来作为独立的接口,比如只读集合XXX接口,大小不可变集合XXX。但是,这将极大地增加API的篇幅,API的作者们就受不了了。
我们自己的设计是否也应该时不时地加上些“可选”操作呢?我并不这么认为。尽管集合类库应用广泛,人尽皆知。但是这个类库的编码风格在其他问题领域里并不常见。(集合类库的设计者面临大量彼此对立的要求。用户希望它学得轻松用着上手,完全支持泛型,语意清晰,同时又希望它像手写的代码一样高效。显然这些要求不可能同时被满足。)在我们自己的编程任务中,很少会遇到如此极端的限定。即使不使用可选的接口方法这种奇葩方案,你也可以干好自己的活。

java.util.Collections (from Version 1.2)

static <E> Collection unmodifiableCollection(Collection<E> c)
static <E> List unmodifiableList(List<E> c)
static <E> Set unmodifiableSet(Set<E> c)
static <E> SortedSet unmodifiableSortedSet(SortedSet<E> c)
static <E> SortedSet unmodifiableNavigableSet(NavigableSet<E> c)
static <K, V> Map unmodifiableMap(Map<K,V> c)
static <K, V> SortedMap unmodifiableSortedMap(SortedMap<K,V> c)
static <K, V> SortedMap unmodifiableNavigableMap(NavigableMap<K,V> c)
创建集合的一个不可修改的视窗;对该视窗使用的mutator方法将抛出UnSupportedOperationException.
static <E> Collection<Eg> synchronizedCollection(Collection<E> c)
static <E>List synchronizedList(List<E> c)
static <E> Set synchronizedSet(Set<E> c)
static <E> SortedSet synchronizedSortedSet(SortedSet<E> c)
static <E> NavigableSet synchronizedNavigableSet(NavigableSet<E> c)
static <K, V> Map<K,V> synchronizedMap(Map<K,V> c)
static <K, V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> c)
static <K, V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> c)
创建集合的一个视窗,对该视窗使用的方法是同步的。
static <E> Collection checkedCollection(Collection<E> c, Class<E> elementType)
static <E> List checkedList(List<E> c, Class<E> elementType)
static <E> Set checkedSet(Set<E> c, Class<E> elementType)
static <E> SortedSet checkedSortedSet(SortedSet<E> c, Class<E> elementType)
static <E> NavigableSet checkedNavigableSet(NavigableSet<E> c, Class<E> elementType)
static <K, V> Map checkedMap(Map<K,V> c,Class<K> keyType, Class<V> valueType)
static <K, V> SortedMap checkedSortedMap(SortedMap<K,V> c,Class<K> keyType, Class<V> valueType)
static <K, V> NavigableMap checkedNavigableMap(NavigableMap<K,V> c,Class<K> keyType, Class<V> valueType)
static <E> Queue <E> checkedQueue(Queue<E> c,Class<E> elementType)
创建集合的一个视窗,当将错误类型的元素插入该视窗时,方法会抛出ClassCastException
static <E> List<E> nCopies(int n, E value)
static <E> Set<E> singleton( E value)
static <E> List<E> singletonList( E value)
static <K, V> Map<K, V> singletonMap( K key, V value)
将一个元素填充为不可修改的具有n个E元素的List视图,或用一个元素构建单例Set,List,或Map视图
static<E> List<E> emptyList()
static<E> Set<E> emptySet()
static<E> SortedSet<E> emptySortedSet()
static<E> NavigableSet<E> emptyNavigableSet()
static <K, V> Map<K, V> emptyMap( )
static <K, V> SortedMap<K, V> emptySortedMap( )
static <K, V> NavigableMap<K, V> emptyNavigableMap( )
static<T> Enumeration<T> emptyEnumeration()
static<T> Iterator<T> emptyIterator()
static<T> ListIterator<T> emptyListIterator()
创建空的collection,map或iterator

java.util.Arrays (from Version 1.2)

static<E> List<E> asList(E…arrays)
并返回该数组的List视图,该视图可修改元素,但不可以改变结构(增减元素)

java.util.List<E> (from Version 1.2)

List<E> subList(int firstIncluded, int firstExcluded)
返回List内指定范围的sublist视图

java.util.SortedSet<E> (from Version 1.2)

SortedSet<E> subSet(E firstIncluded, E firstExcluded)
SortedSet<E> headSet(E firstExcluded)
SortedSet<E> tailSet(E firstIncluded)
返回指定区域的subSortedSet视图

java.util.NavigableSet<E> (from Version 6)

NavigableSet<E> subSet(E from, boolean fromIncluded, E to, boolean toIncluded)
NavigableSet<E> headSet( E to, boolean toIncluded)
NavigableSet<E> tailSet(E from, boolean fromIncluded)
返回原NavigableSet的子集视窗,boolean标签可以决定是否包含边界元素

java.util.SortedMap<K,V> (from Version 1.2)

SortedMap<K,V> subMap(K firstIncluded, K fristExcluded)
SortedMap<K,V>headMap(K fristExcluded)
SortedMap<K,V> tailMap(K firstIncluded)
返回key值位于指定范围的SortedSet子集视窗

java.util.NavigableMap<K,V> (from Version 6)

NavigableMap<K,V> subMap(K from, boolean fromIncluded, K to, boolean toIncluded)
NavigableMap<K,V> headMap( K to, boolean toIncluded)
NavigableMap<K,V> tailMap(K from, boolean fromIncluded)
返回key值位于指定区间的元素的NavigableMap视图,boolean标签指定是否包含边界元素

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值