Wrapper implementations in the java.util collections
来源:互联网 发布:navicat 执行sql语句 编辑:程序博客网 时间:2024/06/05 06:26
From Java online tutorial
Wrapper Implementations
Wrapper implementations delegate all their real work to a specified collection but add extra functionality on top of what this collection offers. For design pattern fans, this is an example of the decorator pattern. Although it may seem a bit exotic, it's really pretty straightforward.These implementations are anonymous; rather than providing a public class, the library provides a static factory method. All these implementations are found in the
Collections
class, which consists solely of static methods.
Synchronization Wrappers
The synchronization wrappers add automatic synchronization (thread-safety) to an arbitrary collection. Each of the six core collection interfaces —Collection
,Set
,List
,Map
,SortedSet
, andSortedMap
— has one static factory method.Each of these methods returns a synchronized (thread-safe)public static <T> Collection<T> synchronizedCollection(Collection<T> c);public static <T> Set<T> synchronizedSet(Set<T> s);public static <T> List<T> synchronizedList(List<T> list);public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m);public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s);public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m);
Collection
backed up by the specified collection. To guarantee serial access, all access to the backing collection must be accomplished through the returned collection. The easy way to guarantee this is not to keep a reference to the backing collection. Create the synchronized collection with the following trick.A collection created in this fashion is every bit as thread-safe as a normally synchronized collection, such as aList<Type> list = Collections.synchronizedList(new ArrayList<Type>());
Vector
.In the face of concurrent access, it is imperative that the user manually synchronize on the returned collection when iterating over it. The reason is that iteration is accomplished via multiple calls into the collection, which must be composed into a single atomic operation. The following is the idiom to iterate over a wrapper-synchronized collection.
If an explicit iterator is used, theCollection<Type> c = Collections.synchronizedCollection(myCollection);synchronized(c) { for (Type e : c) foo(e);}
iterator
method must be called from within thesynchronized
block. Failure to follow this advice may result in nondeterministic behavior. The idiom for iterating over aCollection
view of a synchronizedMap
is similar. It is imperative that the user synchronize on the synchronizedMap
when iterating over any of itsCollection
views rather than synchronizing on theCollection
view itself, as shown in the following example.One minor downside of using wrapper implementations is that you do not have the ability to execute any noninterface operations of a wrapped implementation. So, for instance, in the precedingMap<KeyType, ValType> m = Collections.synchronizedMap(new HashMap<KeyType, ValType>()); ...Set<KeyType> s = m.keySet(); ...synchronized(m) { // Synchronizing on m, not s! while (KeyType k : s) foo(k);}
List
example, you cannot callArrayList
'sensureCapacity
operation on the wrappedArrayList
.
Unmodifiable Wrappers
Unlike synchronization wrappers, which add functionality to the wrapped collection, the unmodifiable wrappers take functionality away. In particular, they take away the ability to modify the collection by intercepting all the operations that would modify the collection and throwing anUnsupportedOperationException
. Unmodifiable wrappers have two main uses, as follows:Like synchronization wrappers, each of the six core
- To make a collection immutable once it has been built. In this case, it's good practice not to maintain a reference to the backing collection. This absolutely guarantees immutability.
- To allow certain clients read-only access to your data structures. You keep a reference to the backing collection but hand out a reference to the wrapper. In this way, clients can look but not modify, while you maintain full access.
Collection
interfaces has one static factory method.public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c);public static <T> Set<T> unmodifiableSet(Set<? extends T> s);public static <T> List<T> unmodifiableList(List<? extends T> list);public static <K,V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m);public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<? extends T> s);public static <K,V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, ? extends V> m);
Checked Interface Wrappers
TheCollections.checked
interface wrappers are provided for use with generic collections. These implementations return a dynamically type-safe view of the specified collection, which throws aClassCastException
if a client attempts to add an element of the wrong type. The generics mechanism in the language provides compile-time (static) type-checking, but it is possible to defeat this mechanism. Dynamically type-safe views eliminate this possibility entirely.
- Wrapper implementations in the java.util collections
- java.util.Collections;Collections .synchronizedMap
- java.util.Collections学习
- java.util.Collections类
- java.util.Collections学习
- java.util.Collections使用说明
- java.util.Collections
- java.util.Collections 类
- java.util.Collections
- java.util.Collections.reverse()
- Java.util.Collections 类
- java.util.Collections
- 9. java.util.Collections
- util.Collection与java.util.Collections区别
- Java.util.Collections类包
- 要点Java20 java.util.Collections
- java.util.Collections方法大全
- java.util.Collections空集合
- 动态生成rabots
- 左外连接
- 为Mnemosyne写的一个自动给卡片加音标的小程序
- 面试题Struts_Hibernate_Spring_Css
- TelephonyManager类使用方法大全
- Wrapper implementations in the java.util collections
- 金额转换,阿拉伯数字的金额转换成中国传统的形式
- Python 不支持杀死子线程
- SQL Server BOM展开方法整理
- 【zz】CentOS 5 常见的configure error的解决方法
- csnd上开个博客,从2003年上csdn有8年了,也在这里抢个地方。
- java.math.BigDecimal类的用法
- 如何建立菜单
- 用java判断是否整数