Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.map

public Class Flat3Map<K, V>

extends Object
implements IterableMap<K, V>, Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable, org.apache.commons.collections4.IterableMap, org.apache.commons.collections4.IterableGet, org.apache.commons.collections4.Get, org.apache.commons.collections4.Put, java.util.Map
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.AbstractCollection, .AbstractSet, .Collection, .Iterator, .Map, .NoSuchElementException, .Set, org.apache.commons.collections4.IterableMap, .MapIterator, .ResettableIterator, org.apache.commons.collections4.iterators.EmptyIterator, .EmptyMapIterator

A Map implementation that stores data in simple fields until the size is greater than 3.

This map is designed for performance and can outstrip HashMap. It also has good garbage collection characteristics.

The design uses two distinct modes of operation - flat and delegate. While the map is size 3 or less, operations map straight onto fields using switch statements. Once size 4 is reached, the map switches to delegate mode and only switches back when cleared. In delegate mode, all operations are forwarded straight to a HashMap resulting in the 5% performance loss.

The performance gains on puts are due to not needing to create a Map Entry object. This is a large saving not only in performance but in garbage collection.

Whilst in flat mode this map is also easy for the garbage collector to dispatch. This is because it contains no complex objects or arrays which slow the progress.

Do not use Flat3Map if the size is likely to grow beyond 3.

Note that Flat3Map is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv abstract static class
pack-priv static class
Flat3Map.EntrySet<K, V>

EntrySet

pack-priv static class
Flat3Map.EntrySetIterator<K, V>

EntrySetIterator and MapEntry

pack-priv static class
pack-priv static class
Flat3Map.FlatMapIterator<K, V>

FlatMapIterator

pack-priv static class
Flat3Map.KeySet<K>

KeySet

pack-priv static class
Flat3Map.KeySetIterator<K>

KeySetIterator

pack-priv static class
Flat3Map.Values<V>

Values

pack-priv static class
Flat3Map.ValuesIterator<V>

ValuesIterator

Field Summary

Modifier and TypeField and Description
private transient AbstractHashedMap<K, V>
delegateMap

Map, used while in delegate mode

private transient int
hash1

Hash, used while in flat mode

private transient int
hash2

Hash, used while in flat mode

private transient int
hash3

Hash, used while in flat mode

private transient K
key1

Key, used while in flat mode

private transient K
key2

Key, used while in flat mode

private transient K
key3

Key, used while in flat mode

private static final long
serialVersionUID

Serialization version

private transient int
size

The size of the map, used while in flat mode

private transient V
value1

Value, used while in flat mode

private transient V
value2

Value, used while in flat mode

private transient V
value3

Value, used while in flat mode

Constructor Summary

AccessConstructor and Description
public
Flat3Map()

Constructor.

public
Flat3Map(final Map<? extends K, ? extends V>
the map to copy
map
)

Constructor copying elements from another map.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements java.util.Map.clear, org.apache.commons.collections4.Put.clear.

Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
public Flat3Map<K, V>

Returns:

a shallow clone
clone
()

Overrides java.lang.Object.clone.

Clones the map without cloning the keys or values.
public boolean

Returns:

true if the map contains the key
containsKey
(final Object
the key to search for
key
)

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether the map contains the specified key.
public boolean

Returns:

true if the map contains the key
containsValue
(final Object
the value to search for
value
)

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether the map contains the specified value.
private void
convertToMap()

Converts the flat map data to a map.

protected AbstractHashedMap<K, V>

Returns:

a new AbstractHashedMap or subclass
createDelegateMap
()

Create an instance of the map used for storage when in delegation mode.

public Set<Map.Entry<K, V>>

Returns:

the entrySet view
entrySet
()

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Gets the entrySet view of the map.
public boolean

Returns:

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

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares this map with another.
public V

Returns:

the mapped value, null if no match
get
(final Object
the key
key
)

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value mapped to the key specified.
public int

Returns:

the hash code defined in the Map interface
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Gets the standard Map hashCode.
public boolean

Returns:

true if the map is currently size zero
isEmpty
()

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is currently empty.
public Set<K>

Returns:

the keySet view
keySet
()

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Gets the keySet view of the map.
public MapIterator<K, V>

Returns:

the map iterator
mapIterator
()

Implements org.apache.commons.collections4.IterableGet.mapIterator.

Gets an iterator over the map.
public V

Returns:

the value previously mapped to this key, null if none
put
(final K
the key to add
key
,
final V
the value to add
value
)

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts a key-value mapping into this map.
public void
putAll(final Map<? extends K, ? extends V>
the map to add
map
)

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Puts all the values from the specified map into this map.
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

public V

Returns:

the value mapped to the removed key, null if key not in map
remove
(final Object
the mapping to remove
key
)

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Removes the specified mapping from this map.
public int

Returns:

the size
size
()

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Gets the size of the map.
public String

Returns:

a string version of the map
toString
()

Overrides java.lang.Object.toString.

Gets the map as a String.
public Collection<V>

Returns:

the values view
values
()

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Gets the values view of the map.
private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait