Top Description Fields Constructors Methods
org.apache.commons.collections4.keyvalue

public Class MultiKey<K>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<K>
the type of keys
Imports
java.io.Serializable, java.util.Arrays

A MultiKey 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 look up a file name 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 retrieve the localized text
MultiKey multiKey = new MultiKey(key, locale);
String localizedText = (String) map.get(multiKey);
Since
3.0

Field Summary

Modifier and TypeField and Description
private transient int
hashCode

The cached hashCode

private final K[]
keys

The individual keys

private static final long
serialVersionUID

Serialisation version

Constructor Summary

AccessConstructor and Description
public
MultiKey(final K
the first key
key1
,
final K
the second key
key2
)

Constructor taking two keys.

public
MultiKey(final K
the first key
key1
,
final K
the second key
key2
,
final K
the third key
key3
)

Constructor taking three keys.

public
MultiKey(final K
the first key
key1
,
final K
the second key
key2
,
final K
the third key
key3
,
final K
the fourth key
key4
)

Constructor taking four keys.

public
MultiKey(final K
the first key
key1
,
final K
the second key
key2
,
final K
the third key
key3
,
final K
the fourth key
key4
,
final K
the fifth key
key5
)

Constructor taking five keys.

public
MultiKey(final K[]
the array of keys, not null
keys
)

Constructor taking an array of keys which is cloned.

public
MultiKey(final K[]
the array of keys, not null
keys
,
final boolean
true to clone the array, false to assign it
makeClone
)

Constructor taking an array of keys, optionally choosing whether to clone.

Method Summary

Modifier and TypeMethod and Description
private void
calculateHashCode(final Object[]
the keys to calculate the hash code for
keys
)

Calculate the hash code of the instance using the provided keys.

public boolean

Returns:

true if equal
equals
(final Object
the other object to compare to
other
)

Overrides java.lang.Object.equals.

Compares this object to another.
public K

Returns:

the key at the index
getKey
(final int
the index to retrieve
index
)

Gets the key at the specified index.

public K[]

Returns:

the individual keys
getKeys
()

Gets a clone of the array of keys.

public int

Returns:

the hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Gets the combined hash code that is computed from all the keys.
protected Object

Returns:

the instance with recalculated hash code
readResolve
()

Recalculate the hash code after deserialization.

public int

Returns:

the size of the list of keys
size
()

Gets the size of the list of keys.

public String

Returns:

a debugging string
toString
()

Overrides java.lang.Object.toString.

Gets a debugging string version of the key.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

hashCodeback to summary
private transient int hashCode

The cached hashCode

keysback to summary
private final K[] keys

The individual keys

serialVersionUIDback to summary
private static final long serialVersionUID

Serialisation version

Constructor Detail

MultiKeyback to summary
public MultiKey(final K key1, final K key2)

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:K

the first key

key2:K

the second key

Annotations
@SuppressWarnings:unchecked
MultiKeyback to summary
public MultiKey(final K key1, final K key2, final K key3)

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:K

the first key

key2:K

the second key

key3:K

the third key

Annotations
@SuppressWarnings:unchecked
MultiKeyback to summary
public MultiKey(final K key1, final K key2, final K key3, final K key4)

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:K

the first key

key2:K

the second key

key3:K

the third key

key4:K

the fourth key

Annotations
@SuppressWarnings:unchecked
MultiKeyback to summary
public MultiKey(final K key1, final K key2, final K key3, final K key4, final K key5)

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:K

the first key

key2:K

the second key

key3:K

the third key

key4:K

the fourth key

key5:K

the fifth key

Annotations
@SuppressWarnings:unchecked
MultiKeyback to summary
public MultiKey(final K[] keys)

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:K[]

the array of keys, not null

Exceptions
IllegalArgumentException:
if the key array is null
MultiKeyback to summary
public MultiKey(final K[] keys, final boolean makeClone)

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:K[]

the array of keys, not null

makeClone:boolean

true to clone the array, false to assign it

Exceptions
IllegalArgumentException:
if the key array is null
Since
3.1

Method Detail

calculateHashCodeback to summary
private void calculateHashCode(final Object[] keys)

Calculate the hash code of the instance using the provided keys.

Parameters
keys:Object[]

the keys to calculate the hash code for

equalsback to summary
public boolean equals(final Object other)

Overrides java.lang.Object.equals.

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.

Parameters
other:Object

the other object to compare to

Returns:boolean

true if equal

Annotations
@Override
getKeyback to summary
public K getKey(final int index)

Gets the key at the specified index.

The key should be immutable. If it is not then it must not be changed.

Parameters
index:int

the index to retrieve

Returns:K

the key at the index

Exceptions
IndexOutOfBoundsException:
if the index is invalid
Since
3.1
getKeysback to summary
public K[] getKeys()

Gets a clone of the array of keys.

The keys should be immutable If they are not then they must not be changed.

Returns:K[]

the individual keys

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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.

Returns:int

the hash code

Annotations
@Override
readResolveback to summary
protected Object readResolve()

Recalculate the hash code after deserialization. The hash code of some keys might have change (hash codes based on the system hash code are only stable for the same process).

Returns:Object

the instance with recalculated hash code

sizeback to summary
public int size()

Gets the size of the list of keys.

Returns:int

the size of the list of keys

Since
3.1
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets a debugging string version of the key.

Returns:String

a debugging string

Annotations
@Override