29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.HashMap;
32import java.util.Iterator;
63 implements Cloneable, Map<K, V>
74 private final HashMap<K,V> map;
75 private final ArrayList<V> data;
76 private final boolean supportNullValue;
87 public ArrayHashMap(
final boolean supportNullValue,
final int initialCapacity,
final float loadFactor) {
88 this.map =
new HashMap<K,V>(initialCapacity, loadFactor);
89 this.data =
new ArrayList<V>(initialCapacity);
90 this.supportNullValue = supportNullValue;
97 map =
new HashMap<K, V>(o.map);
98 data =
new ArrayList<V>(o.data);
99 supportNullValue = o.supportNullValue;
129 public final ArrayList<V>
getData() {
return data; }
136 return new ArrayList<V>(data);
140 public final HashMap<K,V>
getMap() {
return map; }
143 public final String
toString() {
return data.toString(); }
175 return map.entrySet();
179 public final V
get(
final Object key) {
192 public final V
put(
final K key,
final V value)
throws NullPointerException {
194 if( supportNullValue ) {
196 final boolean exists = map.containsKey(key);
199 if(
null != ( oldValue = map.put(key, value) ) ) {
201 throw new InternalError(
"Already existing, but checked before: "+key+
" -> "+oldValue);
205 oldValue = map.put(key, value);
206 if( !data.remove(oldValue) ) {
207 throw new InternalError(
"Already existing, but not in list: "+oldValue);
211 checkNullValue(value);
213 if(
null != ( oldValue = map.put(key, value) ) ) {
215 if( !data.remove(oldValue) ) {
216 throw new InternalError(
"Already existing, but not in list: "+oldValue);
220 if(!data.add(value)) {
221 throw new InternalError(
"Couldn't add value to list: "+value);
227 public void putAll(
final Map<? extends K, ? extends V> m) {
228 for (
final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
229 final Map.Entry<? extends K, ? extends V> e = i.next();
230 put(e.getKey(), e.getValue());
242 public final V
remove(
final Object key) {
243 if( supportNullValue ) {
244 if( map.containsKey(key) ) {
246 final V oldValue = map.remove(key);
247 if ( !data.remove(oldValue) ) {
248 throw new InternalError(
"Couldn't remove prev mapped pair: "+key+
" -> "+oldValue);
254 if (
null != (oldValue = map.remove(key) ) ) {
256 if ( !data.remove(oldValue) ) {
257 throw new InternalError(
"Couldn't remove prev mapped pair: "+key+
" -> "+oldValue);
267 return map.containsKey(key);
272 return map.containsValue(value);
276 public final boolean equals(
final Object arrayHashMap) {
285 return map.hashCode();
290 return data.isEmpty();
298 private static final void checkNullValue(
final Object value)
throws NullPointerException {
299 if(
null == value ) {
300 throw new NullPointerException(
"Null value not supported");
HashMap implementation backed by an ArrayList to preserve order of values.
boolean containsValue(final Object value)
final boolean equals(final Object arrayHashMap)
final ArrayList< V > toArrayList()
void putAll(final Map<? extends K, ? extends V > m)
ArrayHashMap(final boolean supportNullValue, final int initialCapacity, final float loadFactor)
final boolean supportsNullValue()
Returns true for default behavior, i.e.
final V put(final K key, final V value)
static final float DEFAULT_LOAD_FACTOR
Default load factor: {@value}.
final boolean containsKey(final Object key)
final HashMap< K, V > getMap()
Returns this object hash map.
ArrayHashMap(final ArrayHashMap< K, V > o)
final Object clone()
Implementation uses ArrayHashMap(ArrayHashMap).
Set< java.util.Map.Entry< K, V > > entrySet()
static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity: {@value}.
final ArrayList< V > getData()
Returns this object ordered ArrayList.