I dislike the map implementation in Java because it instantiates a new Iterator & Entry all the time. Here’s an implementation of my own Map, it also has way more functionality. For example, you can multiple entries with the same key, and there are various methods to manipulate those entries.
Here’s the output comparing HashMap and my implementation:
Mine obviously is only slightly better, but given all the functionality I added I’m kinda suprised I can easily match HashMap in speed.
Here’s the description of the class:
Here are it’s method signatures:
public class CyclicMap<K, V>
{
public CyclicMap( int tablePower )
public void add(final K key, final V value)
public void addAll(final Map<K, V> map)
public void addAll(final CyclicMap<K, V> map)
public boolean addIfAbsent(final K key, final V value)
public V put(final K key, final V value)
public <D extends Collection<V>> D putAll(final CyclicMap<K, V> map, final D destination)
public <D extends Collection<V>> D putAll(final Map<K, V> map, final D destination)
public V get(final K key)
public <D extends Collection<V>> D getAll(final K key, final D destination)
public K getKey(final V value)
public <D extends Collection<K>> D getAllKeys(final V value, final D destination)
public V remove(final K key)
public void removeAll(final K key)
public void removeAll(final K ... keyArray)
public void removeAll(final Iterable<K> keyIterable)
public int countOf(final K key)
public K removeValue(final V value)
public void removeAllValue(final V value)
public void removeAllValue(final V ... valueyArray)
public void removeAllValue(final Iterable<V> valueIterable)
public int countOfValue(final V value)
public boolean has(final K key)
public boolean hasValue(final V value)
public Entry<K, V> beforeKey(final K key)
public Entry<K, V> beforeValue(final V value)
public int hash(final K key)
public K removeFirstKey()
public V removeFirstValue()
public Entry<K, V> removeFirstEntry()
public Iterable<Entry<K, V>> entries()
public Iterable<Entry<K, V>> entriesWithKey(final K key)
public Iterable<Entry<K, V>> entriesWithValue(final V value)
public Iterable<V> values()
public Iterable<K> keys()
public void clear( final boolean freeEntries )
public int size()
public boolean isEmpty()
public void free( final Entry<K, V> entry )
public static void removeAllFromIterator( final Iterable<?> iterable )
public static <E, C extends Collection<E>> C removeAllFromIterator( final Iterable<E> iterable, final C destination )
public static int countIterator( final Iterable<?> iterable )
public static <K, V, D extends Collection<V>> D stripValues( final Iterable<Entry<K, V>> entries, final D destination )
public static <K, V, D extends Collection<K>> D stripKeys( final Iterable<Entry<K, V>> entries, final D destination )
public static void initPool(final int size)
public static class Entry<K, V>
{
public int hash;
public K key;
public V value;
public Entry<K, V> next;
}
}
Here’s the full code:
http://www.java-gaming.org/?action=pastebin&id=423
Here are the JUnit tests for it:
http://www.java-gaming.org/?action=pastebin&id=425
Let me know how it performs on your machine!
Thanks,
Clicker Monkey