public final class FixedMap<K,V> extends AbstractMap<K,V> implements Indexer<K>
This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects.
This class provides put(Object, Object) operation for the keys
within its fixed key set and permits
null values and the null key. The remove(Object)
operation is not supported. This class guarantees
that the order of the map will remain constant over time.
This class provides log-time performance for the basic
operations (get and put), assuming the system
identity hash function (System.identityHashCode(Object))
disperses elements properly among the buckets.
This implementation is not synchronized, and its iterators are not fail-fast.
keys: set Kmap: keys -> one Vindices: keys lone->one [0..#keys)AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
FixedMap(K[] keys)
Constructs a new fixed map, backed by the given array of keys.
|
FixedMap(Map<K,V> map)
Constructs a new fixed map from the given map.
|
FixedMap(Set<K> keys)
Constructs a new fixed map for the given set of keys.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsKey(Object key)
Tests whether the specified object reference is a key in this fixed map.
|
boolean |
containsValue(Object value)
Tests whether the specified object reference is a value in this fixed map.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a set view of the mappings contained in this map.
|
boolean |
equals(Object o)
Compares the specified object with this map for equality.
|
V |
get(int index)
Returns the value to which the key at the specified index is mapped in this fixed map.
|
V |
get(Object key)
Returns the value to which the specified key is mapped in this fixed map,
or null if the map contains no mapping for
this key.
|
int |
hashCode()
Returns the hash code value for this map.
|
int |
indexOf(K key)
Returns the index of the given key, if it is in this.keys.
|
boolean |
isEmpty() |
K |
keyAt(int index)
Returns the key at the given index.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this fixed
map.
|
V |
remove(Object key)
Throws an
UnsupportedOperationException exception. |
int |
size()
Returns the number of keys in this.indexer.
|
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic FixedMap(K[] keys)
no disj i, j: [0..keys.length) | keys[i] == keys[j]all i, j: [0..keys.length) | i < j => System.identityHashCode(keys[i]) <= System.identityHashCode(keys[j])this.keys' = keys && no this.map'public FixedMap(Map<K,V> map)
this.keys' = keys && this.map = map.mappublic final boolean containsKey(Object key)
containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>public final boolean containsValue(Object value)
containsValue in interface Map<K,V>containsValue in class AbstractMap<K,V>public final Set<Map.Entry<K,V>> entrySet()
Like the backing map, the Map.Entry objects in the set returned by this method define key and value equality as reference-equality rather than object-equality. This affects the behavior of the equals and hashCode methods of these Map.Entry objects. A reference-equality based Map.Entry e is equal to an object o if and only if o is a Map.Entry and e.getKey()==o.getKey() && e.getValue()==o.getValue(). To accommodate these equals semantics, the hashCode method returns System.identityHashCode(e.getKey()) ^ System.identityHashCode(e.getValue()).
Owing to the reference-equality-based semantics of the
Map.Entry instances in the set returned by this method,
it is possible that the symmetry and transitivity requirements of
the Object.equals(Object) contract may be violated if any of
the entries in the set is compared to a normal map entry, or if
the set returned by this method is compared to a set of normal map
entries (such as would be returned by a call to this method on a normal
map). However, the Object.equals contract is guaranteed to
hold among identity-based map entries, and among sets of such entries.
public boolean equals(Object o)
Owing to the reference-equality-based semantics of this map it is possible that the symmetry and transitivity requirements of the Object.equals contract may be violated if this map is compared to a normal map. However, the Object.equals contract is guaranteed to hold among FixedMap instances.
equals in interface Map<K,V>equals in class AbstractMap<K,V>o - object to be compared for equality with this map.Object.equals(Object)public final V get(int index)
index in this.indices[this.keys]IndexOutOfBoundsException - index !in this.indices[this.keys]public final V get(Object key)
public int hashCode()
Object.hashCode().
Owing to the reference-equality-based semantics of the Map.Entry instances in the set returned by this map's entrySet method, it is possible that the contractual requirement of Object.hashCode mentioned in the previous paragraph will be violated if one of the two objects being compared is an FixedMap instance and the other is a normal map.
hashCode in interface Map<K,V>hashCode in class AbstractMap<K,V>Object.hashCode(),
Object.equals(Object),
equals(Object)public final int indexOf(K key)
public final boolean isEmpty()
isEmpty in interface Map<K,V>isEmpty in class AbstractMap<K,V>Map.isEmpty()public final K keyAt(int index)
keyAt in interface Indexer<K>IndexOutOfBoundsException - index !in this.indices[this.keys]public final V put(K key, V value)
put in interface Map<K,V>put in class AbstractMap<K,V>key in this.keysthis.map' = this.map ++ key->valueIllegalArgumentException - key !in this.keyspublic final V remove(Object key)
UnsupportedOperationException exception.remove in interface Map<K,V>remove in class AbstractMap<K,V>Map.remove(java.lang.Object)
© Emina Torlak 2005-present