Class OrderedHashMap<K,V>
java.lang.Object
dev.ultreon.mods.exitconfirmation.OrderedHashMap<K,V>
- All Implemented Interfaces:
Externalizable,Serializable,Cloneable,Map<K,V>
A map of objects whose mapping entries are sequenced based on the order in which they were added. This data structure
has fast O(1) search time, deletion time, and insertion time.
Although this map is sequenced, it cannot implement
This class is not thread safe. When a thread safe implementation is required, use
Although this map is sequenced, it cannot implement
Listbecause of incompatible interface
definitions. The remove methods in List and Map have different return values (see:
List.remove(Object)and Map.remove(Object)).This class is not thread safe. When a thread safe implementation is required, use
Collections.synchronizedMap(Map) as it is documented, or use explicit synchronization controls.- Since:
- 2.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new sequenced hash map with default initial size and load factor.OrderedHashMap(int initialSize) Construct a new sequenced hash map with the specified initial size and default load factor.OrderedHashMap(int initialSize, float loadFactor) Construct a new sequenced hash map with the specified initial size and load factor.OrderedHashMap(Map<K, V> m) Construct a new sequenced hash map and add all the elements in the specified map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()ImplementsMap.clear().clone()Creates a shallow copy of this object, preserving the internal structure by copying only references.booleancontainsKey(Object key) ImplementsMap.containsKey(Object).booleancontainsValue(Object value) ImplementsMap.containsValue(Object).entrySet()ImplementsMap.entrySet().booleanImplementsMap.equals(Object).get(int index) Returns the key at the specified index.ImplementsMap.get(Object).getFirst()Return the entry for the "oldest" mapping.Return the key for the "oldest" mapping.Return the value for the "oldest" mapping.getLast()Return the entry for the "newest" mapping.Return the key for the "newest" mapping.Return the value for the "newest" mapping.getValue(int index) Returns the value at the specified index.inthashCode()ImplementsMap.hashCode().intReturns the index of the specified key.booleanisEmpty()ImplementsMap.isEmpty().iterator()Returns a key iterator.keySet()ImplementsMap.keySet().intlastIndexOf(K key) Returns the last index of the specified key.ImplementsMap.put(Object, Object).voidAdds all the mappings in the specified map to this map, replacing any mappings that already exist (as perMap.putAll(Map)).voidDeserializes this map from the given stream.remove(int index) Removes the element at the specified index.ImplementsMap.remove(Object).sequence()Returns a List view of the keys rather than a set view.intsize()ImplementsMap.size().toString()Provides a string representation of the entries within the map.@NotNull Collection<V> values()ImplementsMap.values().voidSerializes this map to the given stream.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
OrderedHashMap
public OrderedHashMap()Construct a new sequenced hash map with default initial size and load factor. -
OrderedHashMap
public OrderedHashMap(int initialSize) Construct a new sequenced hash map with the specified initial size and default load factor.- Parameters:
initialSize- the initial size for the hash table- See Also:
-
OrderedHashMap
public OrderedHashMap(int initialSize, float loadFactor) Construct a new sequenced hash map with the specified initial size and load factor.- Parameters:
initialSize- the initial size for the hash tableloadFactor- the load factor for the hash table.- See Also:
-
OrderedHashMap
Construct a new sequenced hash map and add all the elements in the specified map. The order in which the mappings in the specified map are added is defined byputAll(Map).
-
-
Method Details
-
size
public int size()ImplementsMap.size(). -
isEmpty
public boolean isEmpty()ImplementsMap.isEmpty(). -
containsKey
ImplementsMap.containsKey(Object).- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
ImplementsMap.containsValue(Object).- Specified by:
containsValuein interfaceMap<K,V>
-
get
ImplementsMap.get(Object). -
getFirst
Return the entry for the "oldest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. This behavior is equivalent to usingentrySet().iterator().next(), but this method provides an optimized implementation.- Returns:
- The first entry in the sequence, or
nullif the map is empty.
-
getFirstKey
Return the key for the "oldest" mapping. That is, return the key for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to usinggetFirst().getKey(), but this method provides a slightly optimized implementation.- Returns:
- The first key in the sequence, or
nullif the map is empty.
-
getFirstValue
Return the value for the "oldest" mapping. That is, return the value for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to usinggetFirst().getValue(), but this method provides a slightly optimized implementation.- Returns:
- The first value in the sequence, or
nullif the map is empty.
-
getLast
Return the entry for the "newest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. The behavior is equivalent to:
Object obj = null; Iterator iter = entrySet().iterator(); while (iter.hasNext()) { obj = iter.next(); } return (Map.Entry) obj;
However, the implementation of this method ensures an O(1) lookup of the last key rather than O(n).- Returns:
- The last entry in the sequence, or
nullif the map is empty.
-
getLastKey
Return the key for the "newest" mapping. That is, return the key for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to usinggetLast().getKey(), but this method provides a slightly optimized implementation.- Returns:
- The last key in the sequence, or
nullif the map is empty.
-
getLastValue
Return the value for the "newest" mapping. That is, return the value for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to usinggetLast().getValue(), but this method provides a slightly optimized implementation.- Returns:
- The last value in the sequence, or
nullif the map is empty.
-
put
ImplementsMap.put(Object, Object). -
remove
ImplementsMap.remove(Object). -
putAll
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as perMap.putAll(Map)). The order in which the entries are added is determined by the iterator returned fromMap.entrySet()for the specified map.- Specified by:
putAllin interfaceMap<K,V> - Parameters:
t- the mappings that should be added to this map.- Throws:
NullPointerException- iftisnull
-
clear
public void clear()ImplementsMap.clear(). -
equals
ImplementsMap.equals(Object). -
hashCode
public int hashCode()ImplementsMap.hashCode(). -
toString
Provides a string representation of the entries within the map. The format of the returned string may change with different releases, so this method is suitable for debugging purposes only. If a specific format is required, useentrySet().iterator()and iterate over the entries in the map formatting them as appropriate. -
keySet
ImplementsMap.keySet(). -
values
ImplementsMap.values(). -
entrySet
ImplementsMap.entrySet(). -
clone
Creates a shallow copy of this object, preserving the internal structure by copying only references. The keys and values themselves are notclone()'d. The cloned object maintains the same sequence.- Overrides:
clonein classObject- Returns:
- A clone of this instance.
- Throws:
CloneNotSupportedException- if clone is not supported by a subclass.
-
get
Returns the key at the specified index.- Throws:
ArrayIndexOutOfBoundsException- if theindexis< 0or>the size of the map.
-
getValue
Returns the value at the specified index.- Throws:
ArrayIndexOutOfBoundsException- if theindexis< 0or>the size of the map.
-
indexOf
Returns the index of the specified key. -
iterator
Returns a key iterator. -
lastIndexOf
Returns the last index of the specified key. -
sequence
Returns a List view of the keys rather than a set view. The returned list is unmodifiable. This is required because changes to the values of the list (usingListIterator.set(Object)) will effectively remove the value from the list and reinsert that value at the end of the list, which is an unexpected side effect of changing the value of a list. This occurs because changing the key, changes when the mapping is added to the map and thus where it appears in the list.
An alternative to this method is to use
keySet()- Returns:
- The ordered list of keys.
- See Also:
-
remove
Removes the element at the specified index.- Parameters:
index- The index of the object to remove.- Returns:
- The previous value corresponding the
key, ornullif none existed. - Throws:
ArrayIndexOutOfBoundsException- if theindexis< 0or>the size of the map.
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException, ClassCastException Deserializes this map from the given stream.- Specified by:
readExternalin interfaceExternalizable- Parameters:
in- the stream to deserialize from- Throws:
IOException- if the stream raises itClassNotFoundException- if the stream raises itClassCastException
-
writeExternal
Serializes this map to the given stream.- Specified by:
writeExternalin interfaceExternalizable- Parameters:
out- the stream to serialize to- Throws:
IOException- if the stream raises it
-