Class MultiKey
- java.lang.Object
-
- org.apache.commons.collections.keyvalue.MultiKey
-
- All Implemented Interfaces:
java.io.Serializable
@Deprecated(since="2021-04-30") public class MultiKey extends java.lang.Object implements java.io.Serializable
Deprecated.Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.AMultiKey
allows multiple map keys to be merged together.The purpose of this class is to avoid the need to write code to handle maps of maps. An example might be the need to lookup a filename by key and locale. The typical solution might be nested maps. This class can be used instead by creating an instance passing in the key and locale.
Example usage:
// populate map with data mapping key+locale to localizedText Map map = new HashMap(); MultiKey multiKey = new MultiKey(key, locale); map.put(multiKey, localizedText); // later retireve the localized text MultiKey multiKey = new MultiKey(key, locale); String localizedText = (String) map.get(multiKey);
- Since:
- Commons Collections 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MultiKey(java.lang.Object[] keys)
Deprecated.Constructor taking an array of keys which is cloned.MultiKey(java.lang.Object[] keys, boolean makeClone)
Deprecated.Constructor taking an array of keys, optionally choosing whether to clone.MultiKey(java.lang.Object key1, java.lang.Object key2)
Deprecated.Constructor taking two keys.MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
Deprecated.Constructor taking three keys.MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
Deprecated.Constructor taking four keys.MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
Deprecated.Constructor taking five keys.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
equals(java.lang.Object other)
Deprecated.Compares this object to another.java.lang.Object
getKey(int index)
Deprecated.Gets the key at the specified index.java.lang.Object[]
getKeys()
Deprecated.Gets a clone of the array of keys.int
hashCode()
Deprecated.Gets the combined hash code that is computed from all the keys.int
size()
Deprecated.Gets the size of the list of keys.java.lang.String
toString()
Deprecated.Gets a debugging string version of the key.
-
-
-
Constructor Detail
-
MultiKey
public MultiKey(java.lang.Object key1, java.lang.Object key2)
Deprecated.Constructor taking two keys.The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
- Parameters:
key1
- the first keykey2
- the second key
-
MultiKey
public MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
Deprecated.Constructor taking three keys.The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key
-
MultiKey
public MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
Deprecated.Constructor taking four keys.The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
-
MultiKey
public MultiKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
Deprecated.Constructor taking five keys.The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
-
MultiKey
public MultiKey(java.lang.Object[] keys)
Deprecated.Constructor taking an array of keys which is cloned.The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
This is equivalent to
new MultiKey(keys, true)
.- Parameters:
keys
- the array of keys, not null- Throws:
java.lang.IllegalArgumentException
- if the key array is null
-
MultiKey
public MultiKey(java.lang.Object[] keys, boolean makeClone)
Deprecated.Constructor taking an array of keys, optionally choosing whether to clone.If the array is not cloned, then it must not be modified.
This method is public for performance reasons only, to avoid a clone. The hashcode is calculated once here in this method. Therefore, changing the array passed in would not change the hashcode but would change the equals method, which is a bug.
This is the only fully safe usage of this constructor, as the object array is never made available in a variable:
new MultiKey(new Object[] {...}, false);
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
- Parameters:
keys
- the array of keys, not nullmakeClone
- true to clone the array, false to assign it- Throws:
java.lang.IllegalArgumentException
- if the key array is null- Since:
- Commons Collections 3.1
-
-
Method Detail
-
getKeys
public java.lang.Object[] getKeys()
Deprecated.Gets a clone of the array of keys.The keys should be immutable If they are not then they must not be changed.
- Returns:
- the individual keys
-
getKey
public java.lang.Object getKey(int index)
Deprecated.Gets the key at the specified index.The key should be immutable. If it is not then it must not be changed.
- Parameters:
index
- the index to retrieve- Returns:
- the key at the index
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is invalid- Since:
- Commons Collections 3.1
-
size
public int size()
Deprecated.Gets the size of the list of keys.- Returns:
- the size of the list of keys
- Since:
- Commons Collections 3.1
-
equals
public boolean equals(java.lang.Object other)
Deprecated.Compares this object to another.To be equal, the other object must be a
MultiKey
with the same number of keys which are also equal.- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- the other object to compare to- Returns:
- true if equal
-
hashCode
public int hashCode()
Deprecated.Gets the combined hash code that is computed from all the keys.This value is computed once and then cached, so elements should not change their hash codes once created (note that this is the same constraint that would be used if the individual keys elements were themselves
Map
keys.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code
-
toString
public java.lang.String toString()
Deprecated.Gets a debugging string version of the key.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a debugging string
-
-