saker.build Documentation TaskDoc JavaDoc Packages
public abstract class SetTransformingMap<E, K, Vextends AbstractMap<K, V>
Pseudo-Map implementation that is backed by a set and dynamically generates the entries for it based on an element value.

This map is immutable, and designed to provide a map implementation that is only used to iterate over its entries. Each entry in this map is dynamically generated based on an entry in the underlying set.

Any method that is not the size(), isEmpty(), Map.forEach(BiConsumer<super K, ? super V>), AbstractMap.values(), AbstractMap.keySet(), entrySet() and their iterator() functions, may throw an UnsupportedOperationException any time.

Important: Implementations should ensure that the transformed keys still stay unique in the map, as they was in the subject set. Violating this may result in undefined behaviour in some implementations.

An use-case for this kind of map is to create a new Map with the given entries without pre-allocating the transformed entries beforehand.

Example:
A new map is created from a set of strings that have the parsed integers as values for it.

 Set<String> set = ...;
 Map<String, Integer> nmap = new TreeMap<>(new SetTransformingMap<String, String, Integer>(set) {
 	@Override
 	protected Map.Entry<String, Integer> transformEntry(String e) {
 		return ImmutableUtils.makeImmutableMapEntry(e, Integer.parseInt(e);
 	}
 });
 
Constructing a map in this way instead of calling Map.put(K, V) for every entry can be more efficient, as new maps can allocate or construct their instances more efficiently.
EThe element type of the set.
KThe key type of this map.
VThe value type of this map.
Fields
protected final Set<extends E>
The backing set of the transforming map.
Constructors
public
SetTransformingMap(Set<extends E> set)
Creates a new instance with the given set.
Methods
public Set<Entry<K, V>>
Returns a Set view of the mappings contained in this map.
public boolean
Returns true if this map contains no key-value mappings.
public int
Returns the number of key-value mappings in this map.
protected abstract Entry<K, V>
Transforms the set element to the map entry.
protected final Set<extends E> set
The backing set of the transforming map.
public SetTransformingMap(Set<extends E> set) throws NullPointerException
Creates a new instance with the given set.
setThe subject set.
NullPointerExceptionIf the set is null.
public Set<Entry<K, V>> entrySet()
Overridden from: Map
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
a set view of the mappings contained in this map
public boolean isEmpty()
Overridden from: Map
Returns true if this map contains no key-value mappings.
true if this map contains no key-value mappings
public int size()
Overridden from: Map
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
the number of key-value mappings in this map
protected abstract Entry<K, V> transformEntry(E e)
Transforms the set element to the map entry.
eThe element to transform.
The transformed entry.