The HashNMap can be used to store multiple values by a single key value. The
values stored can be retrieved using a direct query or by creating an
enumeration over the stored elements.
| Method from org.jfree.util.HashNMap Detail: |
public boolean add(Object key,
Object val) {
final List v = (List) this.table.get(key);
if (v == null) {
put(key, val);
return true;
}
else {
return v.add(val);
}
}
Adds a new key/value pair into this map. If the key is not yet in the
map, it gets added to the map and the call is equal to
put(Object,Object). |
public void clear() {
this.table.clear();
}
Clears all keys and values of this map. |
public Object clone() throws CloneNotSupportedException {
final HashNMap map = (HashNMap) super.clone();
map.table = new HashMap();
final Iterator iterator = keys();
while (iterator.hasNext()) {
final Object key = iterator.next();
final List list = (List) map.table.get(key);
if (list != null) {
map.table.put(key, ObjectUtilities.clone(list));
}
}
return map;
}
Creates a deep copy of this HashNMap. |
public boolean contains(Object value) {
if (containsKey(value)) {
return true;
}
return containsValue(value);
}
Tests whether this map contains the given key or value. |
public boolean containsKey(Object key) {
return this.table.containsKey(key);
}
Tests whether this map contains the given key. |
public boolean containsValue(Object value) {
final Iterator e = this.table.values().iterator();
boolean found = false;
while (e.hasNext() && !found) {
final List v = (List) e.next();
found = v.contains(value);
}
return found;
}
Tests whether this map contains the given value. |
public boolean containsValue(Object key,
Object value) {
final List v = (List) this.table.get(key);
if (v == null) {
return false;
}
return v.contains(value);
}
Tests whether this map contains the given value. |
protected List createList() {
return new ArrayList();
}
Returns a new empty list. |
public Object get(Object key,
int n) {
final List v = (List) this.table.get(key);
if (v == null) {
return null;
}
return v.get(n);
}
Retrieves the n-th value registered for an key or null if there was no
such key in the list. An index out of bounds exception is thrown if
there are less than n elements registered to this key. |
public Iterator getAll(Object key) {
final List v = (List) this.table.get(key);
if (v == null) {
return EMPTY_ITERATOR;
}
return v.iterator();
}
Returns an iterator over all elements registered to the given key. |
public Object getFirst(Object key) {
return get(key, 0);
}
Retrieves the first value registered for an key or null if there was no
such key in the list. |
public int getValueCount(Object key) {
if (key == null) {
throw new NullPointerException("Key must not be null.");
}
final List list = (List) this.table.get(key);
if (list != null) {
return list.size();
}
return 0;
}
Returns the number of elements registered with the given key. |
public Set keySet() {
return this.table.keySet();
}
Returns all registered keys as set. |
public Iterator keys() {
return this.table.keySet().iterator();
}
Returns all registered keys as an enumeration. |
public boolean put(Object key,
Object val) {
final List v = (List) this.table.get(key);
if (v == null) {
final List newList = createList();
newList.add(val);
this.table.put(key, newList);
return true;
}
else {
v.clear();
return v.add(val);
}
}
Inserts a new key/value pair into the map. If such a pair already
exists, it gets replaced with the given values. |
public boolean remove(Object key,
Object value) {
final List v = (List) this.table.get(key);
if (v == null) {
return false;
}
if (!v.remove(value)) {
return false;
}
if (v.size() == 0) {
this.table.remove(key);
}
return true;
}
Removes the key/value pair from the map. If the removed entry was the
last entry for this key, the key gets also removed. |
public void removeAll(Object key) {
this.table.remove(key);
}
Removes all elements for the given key. |
public Object[] toArray(Object key) {
if (key == null) {
throw new NullPointerException("Key must not be null.");
}
final List list = (List) this.table.get(key);
if (list != null) {
return list.toArray();
}
return EMPTY_ARRAY;
}
Returns the contents for the given key as object array. If there were
no objects registered with that key, an empty object array is returned. |
public Object[] toArray(Object key,
Object[] data) {
if (key == null) {
throw new NullPointerException("Key must not be null.");
}
final List list = (List) this.table.get(key);
if (list != null) {
return list.toArray(data);
}
if (data.length > 0) {
data[0] = null;
}
return data;
}
Returns the contents for the given key as object array. If there were
no objects registered with that key, an empty object array is returned. |