ariba.util.core
Class MemoryOptimizedMap<K,V>

java.lang.Object
  extended by ariba.util.core.MemoryOptimizedMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public abstract class MemoryOptimizedMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>, java.lang.Cloneable, java.io.Serializable

This is a map implementation that is very memory efficient for very small maps. It is based on the observation that for very small maps, the general overhead can accumulate, especially if you have a lot of them. For example, this is the memory usage for some ways to store 2 pointers: Object w/ 2 members: 16 bytes Object[2] : 24 bytes Object w member which is an Object[2]: 40 bytes This map implementation uses a very simple linear search, which should not be an issue for the sizes in this map. The map switches over to a HashMap if it becomes too large. It behaves exactly like a regular map. Note that 2 methods in the EntrySet are not implemented, however those should be rarely used. Generally one needs to be very aware of how these maps are being used, so this class needs to be very carefully applied. Normally if you feel like you should use this class, you probably did something overall wrong, and should redesign your algorithms ...

See Also:
Serialized Form

Constructor Summary
MemoryOptimizedMap()
           
 
Method Summary
 void clear()
           
 java.lang.Object clone()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
static java.util.Map deepOptimize(java.util.Map tab)
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns a collection view of the mappings contained in this map.
 boolean equals(java.lang.Object that)
          Returns true iff this is equal to that
 V get(java.lang.Object key)
           
static
<K,V> java.util.Map<K,V>
getOptimizedMap(int size)
           
static
<K,V> java.util.Map<K,V>
getOptimizedMap(int size, boolean growOnly)
           
 boolean isEmpty()
           
 java.util.Set<K> keySet()
          Returns a set view of the keys contained in this map.
static
<K,V> java.util.Map<K,V>
optimize(java.util.Map<K,V> tab)
           
static
<K,V> java.util.Map<K,V>
optimize(java.util.Map<K,V> tab, boolean growOnly)
           
static void optimizeInPlace(java.util.Map s)
           
 V put(K key, V value)
           
 void putAll(java.util.Map<? extends K,? extends V> t)
           
 V remove(java.lang.Object key)
           
 int size()
           
 java.lang.String toString()
           
 java.util.Collection<V> values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
hashCode
 

Constructor Detail

MemoryOptimizedMap

public MemoryOptimizedMap()
Method Detail

getOptimizedMap

public static <K,V> java.util.Map<K,V> getOptimizedMap(int size)

getOptimizedMap

public static <K,V> java.util.Map<K,V> getOptimizedMap(int size,
                                                       boolean growOnly)

size

public int size()
Specified by:
size in interface java.util.Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<K,V>

get

public V get(java.lang.Object key)
Specified by:
get in interface java.util.Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface java.util.Map<K,V>

remove

public V remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<K,V>

putAll

public void putAll(java.util.Map<? extends K,? extends V> t)
Specified by:
putAll in interface java.util.Map<K,V>

clear

public void clear()
Specified by:
clear in interface java.util.Map<K,V>

optimize

public static <K,V> java.util.Map<K,V> optimize(java.util.Map<K,V> tab)

optimize

public static <K,V> java.util.Map<K,V> optimize(java.util.Map<K,V> tab,
                                                boolean growOnly)

deepOptimize

public static java.util.Map deepOptimize(java.util.Map tab)

optimizeInPlace

public static void optimizeInPlace(java.util.Map s)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object that)
Returns true iff this is equal to that

Specified by:
equals in interface java.util.Map<K,V>
Overrides:
equals in class java.lang.Object

keySet

public java.util.Set<K> keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
keySet in interface java.util.Map<K,V>
Returns:
a set view of the keys contained in this map.

values

public java.util.Collection<V> values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface java.util.Map<K,V>
Returns:
a collection view of the values contained in this map.

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map<K,V>
Returns:
a collection view of the mappings contained in this map.
See Also:
Map.Entry

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.