Introduction

You can get a LIFO Queue view of a Deque using the asLifoQueue() static method of the Collections class:

<T> Queue<T> asLifoQueue(Deque<T> deque)

To use a Map's implementation as a Set implementation, use the newSetFromMap() static method of the Collections class:

<E> Set<E> newSetFromMap(Map<E, Boolean> map)

To get a weak hash set implementation:

Map map = new WeakHashMap(); // Do not populate and use the map
Set wSet = Collections.newSetFromMap(map); // You can use wSet

To create the set using the WeakHashMap implementation class:

// Do not keep the reference of the Map
Set wSet = Collections.newSetFromMap(new WeakHashMap());

Read-Only Views of Collections

The Collections class offers the following methods to get read-only views of different types of collections:


<K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K,? extends V> m)
      <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
            <T> List<T> unmodifiableList(List<? extends T> list)
         <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
             <T> Set<T> unmodifiableSet(Set<? extends T> s)
    <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)
static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
   <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)

Synchronized View of a Collection

You have one method for each collection type to return the same type of synchronized version of the collection.

<K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m)
      <T> Collection<T> synchronizedCollection(Collection<T> c)
            <T> List<T> synchronizedList(List<T> list)
         <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
    <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)
             <T> Set<T> synchronizedSet(Set<T> s)
       <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
   <K,V> SortedMap<K,V> synchronizedSortedMap (SortedMap<K,V> m)

Related Topic