📄 multihashmap.java
字号:
} else {
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
put(entry.getKey(), entry.getValue());
}
}
}
/**
* Adds a collection of values to the collection associated with the specified key.
*
* @param key the key to store against
* @param values the values to add to the collection at the key, null ignored
* @return true if this map changed
* @since Commons Collections 3.1
*/
public boolean putAll(Object key, Collection values) {
if (values == null || values.size() == 0) {
return false;
}
Collection coll = getCollection(key);
if (coll == null) {
coll = createCollection(values);
if (coll.size() == 0) {
return false;
}
super.put(key, coll);
return true;
} else {
return coll.addAll(values);
}
}
/**
* Checks whether the map contains the value specified.
* <p>
* This checks all collections against all keys for the value, and thus could be slow.
*
* @param value the value to search for
* @return true if the map contains the value
*/
public boolean containsValue(Object value) {
Set pairs = super.entrySet();
if (pairs == null) {
return false;
}
Iterator pairsIterator = pairs.iterator();
while (pairsIterator.hasNext()) {
Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
Collection coll = (Collection) keyValuePair.getValue();
if (coll.contains(value)) {
return true;
}
}
return false;
}
/**
* Checks whether the collection at the specified key contains the value.
*
* @param value the value to search for
* @return true if the map contains the value
* @since Commons Collections 3.1
*/
public boolean containsValue(Object key, Object value) {
Collection coll = getCollection(key);
if (coll == null) {
return false;
}
return coll.contains(value);
}
/**
* Removes a specific value from map.
* <p>
* The item is removed from the collection mapped to the specified key.
* Other values attached to that key are unaffected.
* <p>
* If the last value for a key is removed, <code>null</code> will be returned
* from a subsequant <code>get(key)</code>.
*
* @param key the key to remove from
* @param item the value to remove
* @return the value removed (which was passed in), null if nothing removed
*/
public Object remove(Object key, Object item) {
Collection valuesForKey = getCollection(key);
if (valuesForKey == null) {
return null;
}
boolean removed = valuesForKey.remove(item);
if (removed == false) {
return null;
}
// remove the list if it is now empty
// (saves space, and allows equals to work)
if (valuesForKey.isEmpty()){
remove(key);
}
return item;
}
/**
* Clear the map.
* <p>
* This clears each collection in the map, and so may be slow.
*/
public void clear() {
// For gc, clear each list in the map
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while (pairsIterator.hasNext()) {
Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
Collection coll = (Collection) keyValuePair.getValue();
coll.clear();
}
super.clear();
}
/**
* Gets a collection containing all the values in the map.
* <p>
* This returns a collection containing the combination of values from all keys.
*
* @return a collection view of the values contained in this map
*/
public Collection values() {
Collection vs = values;
return (vs != null ? vs : (values = new Values()));
}
/**
* Gets the values iterator from the superclass, as used by inner class.
*
* @return iterator
*/
Iterator superValuesIterator() {
return super.values().iterator();
}
//-----------------------------------------------------------------------
/**
* Inner class to view the elements.
*/
private class Values extends AbstractCollection {
public Iterator iterator() {
return new ValueIterator();
}
public int size() {
int compt = 0;
Iterator it = iterator();
while (it.hasNext()) {
it.next();
compt++;
}
return compt;
}
public void clear() {
MultiHashMap.this.clear();
}
}
/**
* Inner iterator to view the elements.
*/
private class ValueIterator implements Iterator {
private Iterator backedIterator;
private Iterator tempIterator;
private ValueIterator() {
backedIterator = MultiHashMap.this.superValuesIterator();
}
private boolean searchNextIterator() {
while (tempIterator == null || tempIterator.hasNext() == false) {
if (backedIterator.hasNext() == false) {
return false;
}
tempIterator = ((Collection) backedIterator.next()).iterator();
}
return true;
}
public boolean hasNext() {
return searchNextIterator();
}
public Object next() {
if (searchNextIterator() == false) {
throw new NoSuchElementException();
}
return tempIterator.next();
}
public void remove() {
if (tempIterator == null) {
throw new IllegalStateException();
}
tempIterator.remove();
}
}
//-----------------------------------------------------------------------
/**
* Clones the map creating an independent copy.
* <p>
* The clone will shallow clone the collections as well as the map.
*
* @return the cloned map
*/
public Object clone() {
MultiHashMap cloned = (MultiHashMap) super.clone();
// clone each Collection container
for (Iterator it = cloned.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Collection coll = (Collection) entry.getValue();
Collection newColl = createCollection(coll);
entry.setValue(newColl);
}
return cloned;
}
/**
* Creates a new instance of the map value Collection container.
* <p>
* This method can be overridden to use your own collection type.
*
* @param coll the collection to copy, may be null
* @return the new collection
*/
protected Collection createCollection(Collection coll) {
if (coll == null) {
return new ArrayList();
} else {
return new ArrayList(coll);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -