If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package org.gskbyte.collection;
/*www.java2s.com*/import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Allows some extra methods, just by implementing the abstract method keyForValue()
*
* implements most methods from the List interface
* */publicabstractclass ListHashMap<K, V>
extends ArrayHashMap<K, V>
{
privatestaticfinallong serialVersionUID = -8108026109763603119L;
public ListHashMap()
{ super(); }
public ListHashMap(int capacity)
{ super(capacity); }
public ListHashMap(int capacity, float loadFactor)
{ super(capacity, loadFactor); }
public ListHashMap(Map<K,V> map)
{ super(map); }
/**
* The star method
* */protectedabstract K keyForValue(V value);
public V put(V value)
{ return super.put(keyForValue(value), value); }
// List-like methods
publicboolean add(V value)
{ return add(keyForValue(value), value); }
publicboolean add(int index, V value)
{ return add(index, keyForValue(value), value); }
publicint indexOf(V value)
{ return unmodifiableKeyList().indexOf(keyForValue(value)); }
//true if list changes, false if not
publicboolean addAll(List<V> values)
{
boolean appendsSomething = false;
for(V v : values) {
if( add(v) ) {
appendsSomething = true;
}
}
return appendsSomething;
}
//true if list changed
publicboolean removeAll(Collection<V> values)
{
boolean changed = false;
for(V v : values) {
K key = keyForValue(v);
if( this.remove(key) != null)
changed = true;
}
return changed;
}
//true if list changed
publicboolean retainAll(Collection<V> values)
{
boolean changed = false;
for(V externalValue : values) {
K keyForExternal = keyForValue(externalValue);
if(!containsKey(keyForExternal)) {
remove(keyForExternal);
changed = true;
}
}
return changed;
}
publicboolean containsAllValues(Collection<? extends V> values)
{
for(V v : values) {
if(!containsValue(v))
return false;
}
return true;
}
@SuppressWarnings("unchecked")
@Override
publicboolean containsValue(Object value)
{
return containsKey( keyForValue((V)value) );
}
}